android 内容保存

1.文件
2.sharepreference
3.content provider

1.文件
创建的文件位于/data/data/包名/files/文件名
package com.example.file;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {

	
	private Context context;
	
	public FileService(Context context) {
		super();
		this.context = context;
	}

	/**
	 * 保存文件
	 * @param filename
	 * @param filecontent
	 * @throws FileNotFoundException 
	 */
	public void save(String filename, String filecontent) throws FileNotFoundException {
		// TODO Auto-generated method stub
		

			FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);

			try {
				outStream.write(filecontent.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				outStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	
	public String read(String filename) throws IOException 
	{
		
		FileInputStream inStream = context.openFileInput(filename);
		
		
		//将读出的内容先写入内存,全部读完之后,一块返回
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		
		int len =0;
		while(	(len = inStream.read(buffer)) != -1 )
		{
			outStream.write(buffer,0,len);
			
		}
	
		byte[] data = outStream.toByteArray();
inStream.close();
outStream.close();
		String s=new String(data);
		return s;
	}
	
}


调用
			String filename=mEditName.getText().toString();
				String filecontent=mEditContent.getText().toString();
				
				
				FileService service = new FileService(getApplicationContext());
				try {
					service.save(filename,filecontent);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_SHORT);
					e.printStackTrace();
				}
				
			String filename=mEditName.getText().toString();
				FileService service = new FileService(getApplicationContext());

				try {
					String content=service.read(filename);
					mEditContent.setText(content);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}




2.sharepreference
业务类
package com.example.service;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.EditTextPreference;

public class PreferService {

	private Context context;
	
	public PreferService( Context c) {
		// TODO Auto-generated method stub
		this.context=c;
		
	}
	
	
	/**
	 * 保存参数
	 * @param name
	 * @param age
	 */
	public void save(String name, Integer age) {
		// TODO Auto-generated method stub
		SharedPreferences sh =context.getSharedPreferences("shareprefer", Context.MODE_PRIVATE);//从上下文获取SharedPreferences
		Editor editor = sh.edit();
		
		editor.putString("name", name);
		editor.putInt("age", age);
		
		editor.commit();
	}

	/**
	 * 获取参数
	 * @return
	 */
	public Map<String ,String> getPreference()
	{
		Map<String ,String> map =new  HashMap<String ,String>();
		SharedPreferences sh = context.getSharedPreferences("shareprefer", Context.MODE_PRIVATE);
		map.put("name", sh.getString("name", ""));
		map.put("age", String.valueOf(sh.getInt("age", 0)));
		return map;
	}
}
在activity中调用
package com.example.a;

import java.util.Map;

import com.example.service.PreferService;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button m_bt1;
	private EditText m_edt1;
	private EditText m_edt2;
	
	private PreferService service;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_bt1 = (Button)findViewById(R.id.button1);
        m_edt1 =(EditText)findViewById(R.id.editText1);
        m_edt2 =(EditText)findViewById(R.id.editText2);
        
        
        service =new PreferService(this);//传递一个上下文参数
        //service =new PreferService(getApplicationContext());
        Map<String , String> map = service.getPreference();
        
        m_edt1.setText(map.get("name"));
        m_edt2.setText(map.get("age"));
        
        m_bt1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				service.save(m_edt1.getText().toString(),Integer.parseInt(m_edt2.getText().toString()));
				
				Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
				
			}
		});

    }

    }



你可能感兴趣的:(android 内容保存)