Android中的檔案的讀取方法

來源:互聯網
上載者:User

在Android中根據檔案位置的不同,可分為四種檔案讀取方式,具體如下;

//方法:從resource中的raw檔案夾中擷取檔案並讀取資料,注意:只能讀取不能寫入資料

 public String getFromRaw(int fileId) {    InputStream in = null;    String result = "";    ByteArrayOutputStream baos=null;    try {    // 擷取Resources資源檔流    in = getResources().openRawResource(fileId);    // 檔案大小    int length = in.available();    // 建立byte數組    byte[] buf = new byte[length];    // 建立記憶體流加快效率    baos = new ByteArrayOutputStream();    int count = 0;    while ((count = in.read(buf)) != -1) {    baos.write(buf, 0, count);    }        result=new String(baos.toByteArray(), "UTF-8");    //result = EncodingUtils.getString(buf, "UTF-8");    } catch (IOException e) {    Toast.makeText(this, "找不到檔案", 2000).show();    } finally{    if(baos!=null)    try {    baos.close();    } catch (IOException e) {    e.printStackTrace();    }    if(in!=null)    try {    in.close();    } catch (IOException e) {    e.printStackTrace();    }    }    return result;    }

  
    //方法:從asset中擷取檔案並讀取資料,只能讀取不能寫入資料
  

 public String getFromAsset(String fileName){//fileName="read_asset.txt"        InputStream in = null;    String result = "";    ByteArrayOutputStream baos=null;                try{            in = getAssets().open(fileName);            int size = in.available();                        byte[] buffer = new byte[size];            baos = new ByteArrayOutputStream();                        while(in.read(buffer)!=-1){            baos.write(buffer);            }            // Convert the buffer into a string.         //   result = new String(baos.toByteArray(),"UTF-8");              result = EncodingUtils.getString(baos.toByteArray(), "UTF-8");            }catch(IOException e){            Toast.makeText(this, "找不到檔案", 2000).show();            } finally{    if(baos!=null)    try {    baos.close();    } catch (IOException e) {    e.printStackTrace();    }    if(in!=null)    try {    in.close();    } catch (IOException e) {    e.printStackTrace();    }    }            return result;       }

//方法:向指定系統檔案中寫入指定的資料
  

public void writeFileData(String fileName, String message) {FileOutputStream fos = null;try {fos = openFileOutput(fileName, Context.MODE_PRIVATE);byte[] bytes = message.getBytes();fos.write(bytes);fos.flush();} catch (IOException e) {Toast.makeText(this, "檔案寫入錯誤", 2000).show();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}

   //方法:開啟指定系統檔案讀取其資料,
   

 public String readFileData(String fileName){    FileInputStream fis=null;        String result="";        try{        fis = openFileInput(fileName);        int size = fis.available();        byte [] buffer = new byte[size];        fis.read(buffer);        result=new String(buffer,"UTF-8");        }catch(IOException e){        Toast.makeText(this, "檔案沒有找到", 2000).show();        }finally{        if(fis!=null){        try {fis.close();} catch (IOException e) {e.printStackTrace();}        }        }        return result;//返回讀到的資料字串       }    

ok,下寫到這裡,待續

聯繫我們

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