Android 讀寫檔案方法匯總_Android

來源:互聯網
上載者:User
一、 從resource中的raw檔案夾中擷取檔案並讀取資料(資源檔只能讀不能寫)
複製代碼 代碼如下:

String res = "";
try{
InputStream in = getResources().openRawResource(R.raw.bbi);
//在\Test\res\raw\bbi.txt,
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
//res = EncodingUtils.getString(buffer, "UTF-8");
//res = EncodingUtils.getString(buffer, "UNICODE");
res = EncodingUtils.getString(buffer, "BIG5");
//依bbi.txt的編碼類別型選擇合適的編碼,如果不調整會亂碼
in.close();
}catch(Exception e){
e.printStackTrace();
}

myTextView.setText(res);//把得到的內容顯示在TextView上

二、 從asset中擷取檔案並讀取資料(資源檔只能讀不能寫)
複製代碼 代碼如下:

String fileName = "yan.txt"; //檔案名稱字
String res="";
try{
InputStream in = getResources().getAssets().open(fileName);
// \Test\assets\yan.txt這裡有這樣的檔案存在
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}

三、 從sdcard中去讀檔案,首先要把檔案通過\android-sdk-windows\tools\adb.exe把本機電腦上的檔案copy到sdcard上去,adb.exe push e:/Y.txt /sdcard/, 不可以用adb.exe push e:\Y.txt \sdcard\ 同樣: 把模擬器上的檔案copy到本機電腦上用: adb pull ./data/data/com.tt/files/Test.txt e:/
複製代碼 代碼如下:

String fileName = "/sdcard/Y.txt";
//也可以用String fileName = "mnt/sdcard/Y.txt";
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
//FileInputStream fin = openFileInput(fileName);
//用這個就不行了,必須用FileInputStream
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是可以看到模擬器檔案存放目錄的結構的
複製代碼 代碼如下:

String fileName = "TEST.txt";
String message = "FFFFFFF11111FFFFF" ;
writeFileData(fileName, message);
public voidwriteFileData(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/目錄(相當AP工作目錄)上的檔案,用openFileOutput
複製代碼 代碼如下:

//寫檔案在./data/data/com.tt/files/下面
public voidwriteFileData(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;
}

六、 寫, 讀sdcard目錄上的檔案,要用FileOutputStream, 不能用openFileOutput
複製代碼 代碼如下:

//寫在/mnt/sdcard/目錄下面的檔案
public voidwriteFileSdcard(String fileName,String message){
try{
//FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
FileOutputStream fout = newFileOutputStream(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是任何檔案都可以

聯繫我們

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