Android中的檔案儲存體資料方式 .

來源:互聯網
上載者:User

 

1.檔案儲存體資料使用了Java中的IO操作來進行檔案的儲存和讀取,只不過Android在Context類中封裝好了輸入資料流和輸出資料流的擷取方法。
建立的隱藏檔儲存在/data/data/<package name>/files檔案夾下。

 

 

2.操作。
儲存檔案內容:通過Context.openFileOutput擷取輸出資料流,參數分別為檔案名稱和儲存模式。
讀取檔案內容:通過Context.openFileInput擷取輸入資料流,參數為檔案名稱。
刪除檔案:Context.deleteFile刪除指定的檔案,參數為將要刪除的檔案的名稱。
擷取檔案名稱列表:通過Context.fileList擷取files目錄下的所有檔案名稱數組。
*擷取檔案路徑的方法:
絕對路徑:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以擷取到"/data/data/<package name>/files"

 

3.四種檔案儲存的模式。
Context.MODE_PRIVATE 為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下寫入的內容會覆蓋原檔案的內容。
Context.MODE_APPEND 檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。
MODE_WORLD_READABLE 表示當前檔案可以被其他應用讀取。
MODE_WORLD_WRITEABLE 表示當前檔案可以被其他應用寫入。
在使用模式時,可以用"+"來選擇多種模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);

 

下面通過程式來示範下檔案儲存體的使用。完整代碼下載:android_files.rar

view plaincopy to clipboardprint?
  1. /** 
  2.  * MainActivity 
  3.  *  
  4.  * @author zuolongsnail 
  5.  *  
  6.  */  
  7. public class MainActivity extends Activity {  
  8.     private EditText writeET;  
  9.     private Button writeBtn;  
  10.     private TextView contentView;  
  11.     public static final String FILENAME = "setting.set";  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         writeET = (EditText) findViewById(R.id.write_et);  
  18.         writeBtn = (Button) findViewById(R.id.write_btn);  
  19.         contentView = (TextView) findViewById(R.id.contentview);  
  20.         writeBtn.setOnClickListener(new OperateOnClickListener());  
  21.     }  
  22.   
  23.     class OperateOnClickListener implements OnClickListener {  
  24.         @Override  
  25.         public void onClick(View v) {  
  26.             writeFiles(writeET.getText().toString());  
  27.             contentView.setText(readFiles());  
  28.             System.out.println(getFilesDir());  
  29.         }  
  30.     }  
  31.   
  32.     // 儲存檔案內容   
  33.     private void writeFiles(String content) {  
  34.         try {  
  35.             // 開啟檔案擷取輸出資料流,檔案不存在則自動建立
      
  36.             FileOutputStream fos = openFileOutput(FILENAME,  
  37.                     Context.MODE_PRIVATE);  
  38.             fos.write(content.getBytes());  
  39.             fos.close();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.   
  45.     // 讀取檔案內容   
  46.     private String readFiles() {  
  47.         String content = null;  
  48.         try {  
  49.             FileInputStream fis = openFileInput(FILENAME);  
  50.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  51.             byte[] buffer = new byte[1024];  
  52.             int len = 0;  
  53.             while ((len = fis.read(buffer)) != -1) {  
  54.                 baos.write(buffer, 0, len);  
  55.             }  
  56.             content = baos.toString();  
  57.             fis.close();  
  58.             baos.close();  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         return content;  
  63.     }  
  64. }  

程式:

 

提供一個檔案儲存體資料的工具類:

view plaincopy to clipboardprint?
  1. /** 
  2.  * 檔案儲存體資料方式工具類 
  3.  *  
  4.  * @author zuolongsnail 
  5.  */  
  6. public class FilesUtil {  
  7.     /** 
  8.      * 儲存檔案內容 
  9.      *  
  10.      * @param c 
  11.      * @param fileName 
  12.      *            檔案名稱 
  13.      * @param content 
  14.      *            內容 
  15.      */  
  16.     private void writeFiles(Context c, String fileName, String content, int mode)  
  17.             throws Exception {  
  18.         // 開啟檔案擷取輸出資料流,檔案不存在則自動建立   
  19.         FileOutputStream fos = c.openFileOutput(fileName, mode);  
  20.         fos.write(content.getBytes());  
  21.         fos.close();  
  22.     }  
  23.   
  24.     /** 
  25.      * 讀取檔案內容 
  26.      *  
  27.      * @param c 
  28.      * @param fileName 
  29.      *            檔案名稱 
  30.      * @return 返迴文件內容 
  31.      */  
  32.     private String readFiles(Context c, String fileName) throws Exception {  
  33.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  34.         FileInputStream fis = c.openFileInput(fileName);  
  35.         byte[] buffer = new byte[1024];  
  36.         int len = 0;  
  37.         while ((len = fis.read(buffer)) != -1) {  
  38.             baos.write(buffer, 0, len);  
  39.         }  
  40.         String content = baos.toString();  
  41.         fis.close();  
  42.         baos.close();  
  43.         return content;  
  44.     }  
  45. }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.