編輯:Android 開發實例
Android 中的檔案讀寫有幾種不同的情境:讀取 raw 與 assets 資源檔、讀寫 SD 卡、以及在應用私有目錄中讀寫。本文系統性地整理了這些常見操作的做法。
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);
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(); }
可先用 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(); }
}
寫入與讀取都用 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;
}
讀寫 /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 則可以操作任何檔案,這也是兩者使用時機的關鍵差異。
微信 SDK 調用總是失敗?別忽略 debug 與 release 簽名差異
接入微信 API 時卡了很久,四處找教學、翻官方文檔,文檔寫得相當含糊,只說「三步走」。結果問題根本不在那三步本身,而在一個幾乎沒人提的細節:簽名不一致。 官方說的三步
用 Handler 實現 ViewPager 廣告頁自動輪播的完整方案
很多電商網站和 App 都有自動輪播的商品推廣圖,看起來體驗很不錯。正好今天在學習 ViewPager,於是就動手做了一個功能類似的 Demo。 下面是其中兩個效果的截圖
Android JSON 解析的關鍵細節:從資料結構到型別對應
JSON 是 JavaScript Object Notation 的縮寫,但今天的它早已與 JavaScript 脫鉤,成為一門語言中立的資料交換格式。相較於 XML,
Android JSON 解析實戰:從回應中取出你要的那幾個欄位
JSON(JavaScript Object Notation)早已成為應用程式之間交換資料的通用語言。相較於 XML,它沒有大量冗餘的封閉標籤,結構輕量、可讀性高,行動