Android文件讀寫實例代碼

轉載:http://rnmichelle.javaeye.com/blog/923217

本文轉載自本人在javaeye.com 上發表的原創博客,有興趣的朋友可以看看。

Android文件讀寫實例代碼

1.Manifest文件中權限的聲明
爲了能對sdcard進行讀寫操作,即可創建文件或目錄,需要在AndroidManifest.xml中添加權限的聲明:

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

2.創建目錄及目標文件,並以"utf-8"編碼格式對文件寫入
首先可在目標文件開頭定義一下目錄及文件名常量,方便創建文件時用

  1. final static String FOLDER = "/sample/";   
  2. final static String FILENAME = "sample";    
  3. final static String SUFFIX = ".txt"// suffix could be replaced on demand   

writeFile函數按命名創建目錄和文件並寫入字串

  1. private void writeFile(StringBuilder sb) {
  2. String foldername = Environment.getExternalStorageDirectory().getPath()   
  3.                              + FOLDER;   
  4.     File folder = new File(foldername);   
  5.     if (folder != null && !folder.exists()) {   
  6.         if (!folder.mkdir() && !folder.isDirectory())   
  7.         {   
  8.             Log.d(TAG, "Error: make dir failed!");   
  9.             return;   
  10.         }   
  11.     }   
  12.     
  13.     String stringToWrite = sb.toString();   
  14.     String targetPath = foldername + FILENAME + SUFFIX;   
  15.     File targetFile = new File(targetPath);   
  16.     if (targetFile != null) {   
  17.         if (targetFile.exists()) {   
  18.             targetFile.delete();   
  19.         }   
  20.     
  21.         OutputStreamWriter osw;   
  22.         try{   
  23.             osw = new OutputStreamWriter(   
  24.                         new FileOutputStream(targetFile),"utf-8");   
  25.             try {   
  26.             osw.write(stringToWrite);   
  27.                 osw.flush();   
  28.                 osw.close();   
  29.             } catch (IOException e) {   
  30.                 // TODO Auto-generated catch block   
  31.                 e.printStackTrace();   
  32.             }   
  33.         } catch (UnsupportedEncodingException e1) {   
  34.             // TODO Auto-generated catch block   
  35.             e1.printStackTrace();   
  36.         } catch (FileNotFoundException e1) {   
  37.             // TODO Auto-generated catch block   
  38.             e1.printStackTrace();   
  39.         }   
  40. }   
  41. }

注意在new FileOutputStream時用到編碼方式爲"utf-8",即以"utf-8"格式來保存這個,如果想用別的格式來保存,可換成"GB2312","Big5"等。
此外,待寫入的字串可以將字串樣式先用StringBuilder構造好,直接用StringBuilder變量;也可以直接傳入String類型的變量。

3.以"utf-8"解碼格式讀入文

  1. private String readFile(String filepath) {   
  2.     String path = filepath;   
  3.     if (null == path) {   
  4.         Log.d(TAG, "Error: Invalid file name!");   
  5.         return null;   
  6.     }   
  7.  
  8.     String filecontent = null;   
  9.     File f = new File(path);   
  10.     if (f != null && f.exists())   
  11.     {
  12.         FileInputStream fis = null;   
  13.         try {   
  14.             fis = new FileInputStream(f);   
  15.         } catch (FileNotFoundException e1) {   
  16.             // TODO Auto-generated catch block   
  17.             e1.printStackTrace();   
  18.             Log.d(TAG, "Error: Input File not find!");   
  19.             return null;   
  20.         }   
  21.   
  22.         CharBuffer cb;   
  23.         try {   
  24.             cb = CharBuffer.allocate(fis.available());   
  25.         } catch (IOException e1) {   
  26.             // TODO Auto-generated catch block   
  27.             e1.printStackTrace();   
  28.             Log.d(TAG, "Error: CharBuffer initial failed!");   
  29.             return null;   
  30.         }   
  31.     
  32.         InputStreamReader isr;   
  33.         try {   
  34.             isr = new InputStreamReader(fis, "utf-8");   
  35.             try {   
  36.                 if (cb != null) {   
  37.                    isr.read(cb);   
  38.                 }   
  39.                 filecontent = new String(cb.array());   
  40.                 isr.close();   
  41.             } catch (IOException e) {   
  42.                 e.printStackTrace();   
  43.             }   
  44.         } catch (UnsupportedEncodingException e) {   
  45.             // TODO Auto-generated catch block   
  46.             e.printStackTrace();           
  47.         }   
  48.     }   
  49.     Log.d(TAG, "readFile filecontent = " + filecontent);   
  50.     return filecontent;   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章