数据存储的三种方式

public class SpActivity extends Activity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSp).setOnClickListener(this);
findViewById(R.id.btnSpRead).setOnClickListener(this);
findViewById(R.id.btnFileWrite).setOnClickListener(this);
findViewById(R.id.btnFileReader).setOnClickListener(this);
findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
findViewById(R.id.btnRawRead).setOnClickListener(this);
findViewById(R.id.btnSQlite).setOnClickListener(this);
}

@Override

public class SpActivity extends Activity implements OnClickListener {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.btnSp).setOnClickListener(this);
		findViewById(R.id.btnSpRead).setOnClickListener(this);
		findViewById(R.id.btnFileWrite).setOnClickListener(this);
		findViewById(R.id.btnFileReader).setOnClickListener(this);
		findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
		findViewById(R.id.btnRawRead).setOnClickListener(this);
		findViewById(R.id.btnSQlite).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSp:
			SharedPreferences sp = this.getSharedPreferences("setting", Context.MODE_PRIVATE);
			//SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = sp.edit();
			editor.putString("name", "张三");
			editor.putInt("age", 24);
			editor.putFloat("weight", 110.8f);
			//提交
			editor.commit();
			//editor.apply();
			break;
		case R.id.btnSpRead:
			SharedPreferences spReader = getSharedPreferences("setting", Context.MODE_PRIVATE);
			String name= spReader.getString("name", "N");
			int age =spReader.getInt("age", 18);
			float weight=spReader.getFloat("weight", 80f);
			Toast.makeText(this, name+"--"+age+"--"+weight, Toast.LENGTH_LONG).show();
			break;
		case R.id.btnFileWrite:
			writeFiles();
			break;
		case R.id.btnFileReader:
			readFile();
			break;
		case R.id.btnFileWriteToSd:
			writeFilesToSDCard();
			break;
		case R.id.btnRawRead:
			readRawFile();
			break;
		case R.id.btnSQlite:
			Intent in = new Intent(this, DBOptActivity.class);
			startActivity(in);
			break;
		default:
			break;
		}
	}
	
	
	public void readRawFile(){
		Resources res = getResources();
		InputStream is= res.openRawResource(R.raw.a);
		byte[] buffer = new byte[1024];
		int len=0;
		StringBuilder sb = new StringBuilder();
		try {
			while((len= is.read(buffer))!=-1){
				String tmp = new String(buffer, 0, len);
				sb.append(tmp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
	}
	
	/**
	 * 写文件
	 */
	private void writeFiles(){
		FileOutputStream os =null;
		try {
			os =this.openFileOutput("jerei.txt",Context.MODE_APPEND);
			os.write("姓名:张三".getBytes());
			os.write("年龄:25".getBytes());
			os.write("年龄:25".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(os !=null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				os=null;
			}
		}
	}
	
	public void readFile(){
		FileInputStream is=null;
		StringBuilder sb = new StringBuilder();
		try {
			is = this.openFileInput("jerei.txt");
			byte[] buffer = new byte[1024];
			int len=0;
			while((len= is.read(buffer))!=-1){
				String tmp = new String(buffer,0,len);
				sb.append(tmp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
	}

	public void writeFilesToSDCard(){
		//String filePath = "/mnt/sdcard/jerei";
		String filePath=null;
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			//获取SDCard根路径
			filePath=Environment.getExternalStorageDirectory().toString();
			filePath=filePath+ File.separator+"jerei"+File.separator+"edu";
			File fileParent = new File(filePath);
			if(!fileParent.exists()){
				fileParent.mkdirs();
			}
			
			FileOutputStream os = null;
			try {
				os = new FileOutputStream(new File(fileParent, "a.txt"));
				os.write("向SDCard中写入文件!!".getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(os!=null){
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
}

  

 

转载于:https://www.cnblogs.com/zhongshujunqia/p/3936414.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值