Android開發進階:如何讀寫Android檔案

來源:互聯網
上載者:User

標籤:des   android   c   style   code   java   

Android主要有四大主要組件組成:Activity、ContentProvider、Service、Intent組成。Android檔案的運行主要需要讀寫四大組件的檔案。本文將介紹如何讀寫Android檔案,希望對進行中Android開發的朋友有所協助。

  檔案存放位置

  在Android中檔案的I/O是存放在/data/data/<package name>/file/filename目錄下。

  提示:Android是基於linux系統的,在linux的檔案系統中不存在類似於Windows的磁碟分割現象,其是以一個正斜杠“/”開頭。

  Android中得到輸入輸出資料流

  在Android中,對於流的操作十分簡單。在Context類中有如下兩個方法可以直接得到檔案輸入輸出資料流:

1. public FileInputStream   openFileInput  (String name)  
2. public FileOutputStream openFileOutput (String name, int mode)

  顧名思義,通過如上方法就可以得到檔案輸入輸出資料流。對於第二個方法中的mode,有如下四種模式:

  ◆Use 0 or MODE_PRIVATE( the default operation) :用0表示預設值,只能夠建立檔案的應用程式訪問該檔案,每次檔案寫入為覆蓋方式。

  ◆MODE_APPEND to append to an existing file: 每次檔案寫入為追加方式,類似於StringBuffer中的append()方法。

  ◆MODE_WORLD_READABLE  :只有讀許可權。

  ◆MODE_WORLD_WRITEABLE :只有寫入權限。

  提示:如果想同時得到讀與寫的許可權,則可以在mode處通過如下方式建立:

MODE_WORLD_READABLE+ MODE_WORLD_WRITEABLE 
  對於Java SE部分的補充

  FileOutputStream:

  public void write(byte[] b) throws IOException 該方法可將指定的位元組數組寫入檔案輸出資料流

  FileInputStream:

  public int read(byte[] b) throws IOException 從此輸入資料流中將最多 b.length 個位元組的資料讀入一個 byte 數組中。在某些輸入可用之前,此方法將阻塞。

  對於輸出資料流直接使用write方法即可,可參考如下代碼:

  Java代碼

1. <span style="white-space: pre;"></span>/**   
2. * 寫入資料
3. *
@param fs
4. *
@param content
5.
*/
6. publicvoid fileWrite(FileOutputStream fos,String content){
7. byte[] contentcontentByteArray = content.getBytes();
8. try {
9. fos.write(contentByteArray);
10. } catch (IOException e1) {
11. e1.printStackTrace();
12. }
13. try {//關閉流
14. fos.close();
15. } catch (IOException e) {
16. e.printStackTrace();
17. }
18. }

  對於輸入資料流,出於效能考慮,可先使用ByteArrayOutputStream,向記憶體中建立一個字元數組,當將檔案讀完後,在讀入,參考如下代碼:

  Java代碼

1.           * 讀資料    
2. * @param fis
3. * @return
4. */
5. public String fileRead(FileInputStream fis){
6. ByteArrayOutputStream baos =new ByteArrayOutputStream();
7. byte[] buffer =newbyte[1024];
8. int len =-1;
9. try {
10. while((len=(fis.read(buffer))) !=-1){
11. baos.write(buffer,0,len);
12. }
13. } catch (IOException e) {
14. e.printStackTrace();
15. }
16. String result =new String(baos.toByteArray());
17. //System.out.println(result);
18. try {
19. baos.close();
20. fis.close();
21. } catch (IOException e) {
22. e.printStackTrace();
23. }
24. return result;
25. }

  ByteArrayOutputStream:此類實現了一個輸出資料流,其中的資料被寫入一個 byte 數組。

  public void write(byte[] b,int off,int len) 將指定 byte 數組中從位移量 off 開始的 len 個位元組寫入此 byte 數組輸出資料流。

原文:http://www.codesocang.com/jiaocheng/shoujikaifa/2013/0508/4479.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.