现在的位置: 首页 > 综合 > 正文

Android – 文件读写操作 总结

2014年01月15日 ⁄ 综合 ⁄ 共 2867字 ⁄ 字号 评论关闭

本来打算自己总结一下文件处理的方法,但是没时间,今天碰巧看到有同志总结的还行,于是转载一下:

在android中的文件放在不同位置,它们的读取方式也有一些不同。

本文对android中对资源文件的读取、数据区文件的读取、SD卡文件的读取及RandomAccessFile的方式和方法进行了整理。供参考。

一、资源文件的读取:

      1) 从resource的raw中读取文件数据:

[java] view
plain
copy

  1. String res = "";   
  2. try{   
  3.    
  4.     //得到资源中的Raw数据流  
  5.     InputStream in = getResources().openRawResource(R.raw.test);   
  6.   
  7.     //得到数据的大小  
  8.     int length = in.available();         
  9.   
  10.     byte [] buffer = new byte[length];          
  11.   
  12.     //读取数据  
  13.     in.read(buffer);           
  14.   
  15.     //依test.txt的编码类型选择合适的编码,如果不调整会乱码   
  16.     res = EncodingUtils.getString(buffer, "BIG5");   
  17.       
  18.     //关闭      
  19.     in.close();              
  20.   
  21.    }catch(Exception e){   
  22.       e.printStackTrace();           
  23.    }   


 2) 从resource的asset中读取文件数据

[java] view
plain
copy

  1. String fileName = "test.txt"//文件名字   
  2. String res="";   
  3. try{   
  4.   
  5.    //得到资源中的asset数据流  
  6.    InputStream in = getResources().getAssets().open(fileName);   
  7.   
  8.    int length = in.available();           
  9.    byte [] buffer = new byte[length];          
  10.   
  11.    in.read(buffer);              
  12.    in.close();  
  13.    res = EncodingUtils.getString(buffer, "UTF-8");       
  14.   
  15.   }catch(Exception e){   
  16.   
  17.       e.printStackTrace();           
  18.   
  19.    }   

二、读写/data/data/<应用程序名>目录上的文件:

[java] view
plain
copy

  1. //写数据  
  2. public void writeFile(String fileName,String writestr) throws IOException{   
  3.   try{   
  4.   
  5.         FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);   
  6.   
  7.         byte [] bytes = writestr.getBytes();   
  8.   
  9.         fout.write(bytes);   
  10.   
  11.         fout.close();   
  12.       }   
  13.   
  14.         catch(Exception e){   
  15.         e.printStackTrace();   
  16.        }   
  17. }   
  18.   
  19. //读数据  
  20. public String readFile(String fileName) throws IOException{   
  21.   String res="";   
  22.   try{   
  23.          FileInputStream fin = openFileInput(fileName);   
  24.          int length = fin.available();   
  25.          byte [] buffer = new byte[length];   
  26.          fin.read(buffer);       
  27.          res = EncodingUtils.getString(buffer, "UTF-8");   
  28.          fin.close();       
  29.      }   
  30.      catch(Exception e){   
  31.          e.printStackTrace();   
  32.      }   
  33.      return res;   
  34.   
  35. }     


三、读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件 :

[java] view
plain
copy

  1. //写数据到SD中的文件  
  2. public void writeFileSdcardFile(String fileName,String write_str) throws IOException{   
  3.  try{   
  4.   
  5.        FileOutputStream fout = new FileOutputStream(fileName);   
  6.        byte [] bytes = write_str.getBytes();   
  7.   
  8.        fout.write(bytes);   
  9.        fout.close();   
  10.      }  
  11.   
  12.       catch(Exception e){   
  13.         e.printStackTrace();   
  14.        }   
  15.    }   
  16.   
  17.     
  18. //读SD中的文件  
  19. public String readFileSdcardFile(String fileName) throws IOException{   
  20.   String res="";   
  21.   try{   
  22.          FileInputStream fin = new FileInputStream(fileName);   
  23.   
  24.          int length = fin.available();   
  25.   
  26.          byte [] buffer = new byte[length];   
  27.          fin.read(buffer);       
  28.   
  29.          res = EncodingUtils.getString(buffer, "UTF-8");   
  30.   
  31.          fin.close();       
  32.         }   
  33.   

抱歉!评论已关闭.