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();
				
			}
		});

    }

    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值