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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 15:48   51   0

最经在做项目的时候,遇到了文件的存储读取问题,总体来说,代码实现起来还是挺简单的。数据在移动端的存储其实有很多方式,比如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





分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP