Android本地数据的存储和读取(文件格式)

    最经在做项目的时候,遇到了文件的存储读取问题,总体来说,代码实现起来还是挺简单的。数据在移动端的存储其实有很多方式,比如SharePreference方式,SQLite方式,文件形式。这块儿我主要讲的是以文件的形式存储与读取。

    手机端能放置文件存储的地方有两个,内存与sdcard,以下以函数的形式给出,分别为1写文件在内存,2写文件在sdcard,3读在内存上的文件,4读在sdcard上的文件

1 写文件在内存

 //写文件在 (内存)上采用OpenFileOutput方法
	 public void  WriteFile(String path) throws IOException{
		    
		   int len = 0;
		   String sentence = "sdfsdf";
		   byte[]  buffer = sentence.getBytes();
		   len = buffer.length;
		   FileOutputStream   out  = openFileOutput(path, MODE_PRIVATE);
		   //使用openFileOutput方法内存地址不能有分割符
		   try {
			   out.write(buffer,0 ,len);
			   Log.i(TAG, "文件生成");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			out.close();
		}
		 
		 
	 }
在实现写文件到内存时,我们需要使用的是Andriod自带的openFileOutput方法,不能使用FileOutputStrem(写文件到sdcard),并且参数path只能是类似这种形式(tsd.txt)不能是(/data/data/tsd.txt)这种形式。

2写文件在sdcard上

 //写文件在(Sdcard)上采用FileOutputStream方法 ,注意加权限
	 public void WriteFile1(String path) throws IOException{
		 
		 FileOutputStream  fout = new FileOutputStream(path);
		 byte[]  buffer = "dfgdfgdfg".getBytes();
		 int len = buffer.length;
		 fout.write(buffer, 0, len);
		 fout.close();
		 Log.i(TAG, "成功");
		 
	 }
不能使用openFileoutput方法,只能使用FileOutputStream方法,并且需要添加权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

允许挂载和反挂载文件系统可移动存储。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
模拟器中sdcard中创建文件夹权限

3 读在内存上的文件

 //读文件在(内存)上采用openFileInput的方法
	  
	 public  void  ReadFile(String path) throws IOException
	 {
		 
		 String   content = null  ; 
		 FileInputStream  read = openFileInput(path);
		 byte[]  aaa = new byte[read.available()];
		 read.read(aaa);
		 content = new String(aaa);
         Log.i(TAG, content);
         
         //创建一个内存输出流对象
       /*  ByteArrayOutputStream  outstream = new ByteArrayOutputStream();
         int len = 0; 
         byte []   buffer = new byte[1024];
         try {
			  while((len = read.read(buffer)) != -1){
				  outstream.write(buffer, 0, len);
			  }
			content = outstream.toByteArray();
			Log.i(TAG, new String(content));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		}*/
      
		 
	 }
与写文件在内存上一样,见代码,注释的方法也可以实现,路径与写文件在内存是一样的

4 读在sdcard上的文件

//读文件在(Sdcard)上采用FileInputStream
	 public void ReadFile1(String path){
		     String   contentsd = "";
		  try {
			 
		      FileInputStream  input = new FileInputStream(path);
			  byte[] buffer = new byte[input.available()];
			  input.read(buffer);
			  contentsd = new String(buffer);
			  Log.i(TAG, contentsd);
;			  input.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
	 }
采用FileInputSteram方法
逻辑清楚,代码简单

源代码地址:http://download.csdn.net/detail/danielntz/9505813



 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值