Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 讀寫文件方法大集合

編輯:Android 開發實例

Android 中的檔案讀寫有幾種不同的情境:讀取 raw 與 assets 資源檔、讀寫 SD 卡、以及在應用私有目錄中讀寫。本文系統性地整理了這些常見操作的做法。

一、讀取 raw 資料夾中的檔案

raw 中的資源檔只能讀不能寫。使用 openRawResource() 開啟後讀入位元組,再依編碼類型轉為字串(若編碼不匹配會亂碼):

String res = "";
try{
InputStream in = getResources().openRawResource(R.raw.bbi);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "BIG5");
in.close();
}catch(Exception e){ e.printStackTrace(); }
myTextView.setText(res);

二、讀取 asset 中的檔案

assets 同樣只能讀不能寫:

String fileName = "yan.txt";
String res="";
try{
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(); }

三、從 SD 卡讀取檔案

可先用 adb 把本機檔案 push 到 SD 卡:adb.exe push e:/Y.txt /sdcard/;把模擬器檔案複製回本機則用:adb pull ./data/data/com.tt/files/Test.txt e:/。讀取時務必使用 FileInputStream:

String fileName = "/sdcard/Y.txt";
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(); }
myTextView.setText(res);

四、寫入檔案(應用私有目錄)

一般寫在 \data\data\com.test\files\ 裡,可透過 DDMS 的 File Explorer 查看模擬器檔案目錄結構:

public void writeFileData(String fileName,String message){
try{
FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){ e.printStackTrace(); }
}

五、讀寫 data/data 目錄(相當於 App 工作目錄)

寫入與讀取都用 openFileOutput 與 openFileInput:

//寫文件在 ./data/data/com.tt/files/ 下面
public void writeFileData(String fileName,String message){
try{
FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){ e.printStackTrace(); }
}
//讀文件在 ./data/data/com.tt/files/ 下面
public String readFileData(String fileName){
String res="";
try{
FileInputStream fin = openFileInput(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;
}

六、讀寫 SD 卡目錄

讀寫 /mnt/sdcard/ 目錄時,必須用 FileOutputStream,不能用 openFileOutput:

//寫在 /mnt/sdcard/ 目錄下面的文件
public void writeFileSdcard(String fileName,String message){
try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){ e.printStackTrace(); }
}
//讀在 /mnt/sdcard/ 目錄下面的文件
public String readFileSdcard(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;
}

最後補充一點:openFileOutput 只能操作應用私有目錄(raw 裡編譯過的),而 FileOutputStream 則可以操作任何檔案,這也是兩者使用時機的關鍵差異。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved