Android的檔案操作

來源:互聯網
上載者:User

標籤:android   檔案儲存體   inputstream   outputstream   

Android的檔案操作一、Android的檔案操作簡介

要理解Android的檔案儲存體,首先要明白Android是如何用什麼方式擷取和存入資料的。Android的檔案操作都是通過流的方式進行的,即IO。Android的檔案操作跟Java是一樣的,檔案操作分為輸入資料流(InputStream)和輸出資料流(OutputStream),輸入資料流用於擷取檔案資料,輸出資料流用於隱藏檔。輸入資料流和輸出資料流都是很耗費記憶體空間的,所以在使用Android的IO時,除非特殊需要,一般在使用結束後要記得關閉IO通道。接下來我們就來講解一下,InputStream和OutputStream。

1、InputStream

InputStream是一個抽象類別,其表示位元組輸入資料流的所有類的超類。以下是InputStream經常會使用的一些方法。

 void

close() 
          關閉此輸入資料流並釋放與該流關聯的所有系統資源。

abstract  int

read() 
          從輸入資料流讀取下一個資料位元組。

 int

read(byte[] b) 
          從輸入資料流中讀取一定數量的位元組並將其儲存在緩衝區數組 b 中。

 int

read(byte[] b, int off, int len) 
          將輸入資料流中最多 len 個資料位元組讀入位元組數組。

 

其中close方法需要特別注意,要記得及時調用close方法關閉輸入資料流。

2、OutputStream

OutputStream是一個抽象類別,其表示位元組輸出資料流的所有類的超類。以下是OutputStream經常會使用的一些方法。

 void

close() 
          關閉此輸出資料流並釋放與此流有關的所有系統資源。

 void

write(byte[] b) 
          將 b.length 個位元組從指定的位元組數組寫入此輸出資料流。

 void

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

abstract  void

write(int b) 
          將指定的位元組寫入此輸出資料流。

 

跟InputStream一樣,要記得及時調用close方法關閉輸出資料流。

二、InputStream和OutputStream的擷取和使用。1、操作Android的SD卡的檔案

例如在Android中,需要往android手機的內建記憶體(非SD卡)中讀寫檔案。

Activity的父類 Context 有方法可以擷取輸出和寫入流。
分別是:
openFileOutput(name, mode);
openFileInput(name);
mode有
        Context.MODE_PRIVATE;  //預設,僅此程式私人
        Context.MODE_APPEND;   //追加方式
        Context.MODE_WORLD_READABLE;   //(其它程式)可讀
        Context.MODE_WORLD_WRITEABLE;  //(其它程式)可寫

註:SD卡IO需要許可權:
android.permission.MOUNT_UNMOUNT_FILESYSTEMS
android.permission.WRITE_EXTERNAL_STORAGE

2、使用FileInputStream擷取檔案

在1中我們談到可以用openFileOutput(name, mode)和openFileInput(name)分別擷取輸出資料流和資料輸入流,原Java中提供了FileInputStream和FileOutputStream類,分別繼承於InputStream和OutputStream,在Android通過這個兩個類也是可以擷取輸入資料流和輸出資料流的。

1、FileInputStream的使用方法

以下是FileInputStream的構造方法

FileInputStream(File file) 
          通過開啟一個到實際檔案的串連來建立一個 FileInputStream,該檔案通過檔案系統中的 File 對象 file 指定。

FileInputStream(FileDescriptor fdObj) 
          通過使用檔案描述符 fdObj 建立一個 FileInputStream,該檔案描述符表示到檔案系統中某個實際檔案的現有串連。

FileInputStream(String name) 
          通過開啟一個到實際檔案的串連來建立一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定。

相應代碼:

//newFilename為檔案地址InputStream is= new FileInputStream(newFilename);

2、FileOutputStream的使用方法

以下是FileOutputStream的構造方法

FileOutputStream(File file) 
          建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出資料流。

FileOutputStream(File file, boolean append) 
          建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出資料流。

FileOutputStream(FileDescriptor fdObj) 
          建立一個向指定檔案描述符處寫入資料的輸出檔案流,該檔案描述符表示一個到檔案系統中的某個實際檔案的現有串連。

FileOutputStream(String name) 
          建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流。

FileOutputStream(String name, boolean append) 
          建立一個向具有指定 name 的檔案中寫入資料的輸出檔案流。

 

相應代碼:

// newFilename為要產生的檔案地址OutputStream os = new FileOutputStream(newFilename);


註:同樣需要許可權:
android.permission.MOUNT_UNMOUNT_FILESYSTEMS
android.permission.WRITE_EXTERNAL_STORAGE

3、擷取Android的Assets檔案夾的資料

在Android中,有時候你不只需要在sd卡中擷取檔案,只需要從Assets中擷取檔案。那代碼上可以如此處理。

InputStream is = context.getResources().getAssets().open(mfileName);

 

三、程式碼範例 1、從resource的asset中讀取檔案資料

 

String fileName = "test.txt"; //檔案名稱字String res="";try{    //得到資源中的asset資料流   InputStream in = getResources().getAssets().open(fileName);    int length = in.available();           byte [] buffer = new byte[length];           in.read(buffer);               res = EncodingUtils.getString(buffer, "UTF-8");       }catch(Exception e){       e.printStackTrace();            }


2、讀寫SD卡中的檔案。也就是/mnt/sdcard/目錄下面的檔案 :

//寫資料到SD中的檔案public void writeFileSdcardFile(String fileName,String write_str){ try{        FileOutputStream fout = new FileOutputStream(fileName);       byte [] bytes = write_str.getBytes();        fout.write(bytes);       fout.close();     }       catch(Exception e){        e.printStackTrace();       }   }    //讀SD中的檔案public String readFileSdcardFile(String fileName){  String res="";  try{         FileInputStream fin = new FileInputStream(fileName);          int length = fin.available();          byte [] buffer = new byte[length];         fin.read(buffer);              res = EncodingUtils.getString(buffer, "UTF-8");          fin.close();            }         catch(Exception e){         e.printStackTrace();        }        return res;}


以上內容部分參考網路上的博文,附帶上一個小demo。

http://download.csdn.net/detail/stop_pig/7896851

Android的檔案操作

聯繫我們

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