文件操作

純粹是個(gè)人學(xué)習(xí)總結(jié),如有不對(duì)的地方請(qǐng)吐槽。

一、資源文件的讀?。?/p>

  1. 從resource的raw中讀取文件數(shù)據(jù):
    String res = "";
    try{
    //得到資源中的Raw數(shù)據(jù)流
    InputStream in = getResources().openRawResource(R.raw.test);
    //得到數(shù)據(jù)的大小
    int length = in.available();
    byte [] buffer = new byte[length];
    //讀取數(shù)據(jù)
    in.read(buffer);
    //依test.txt的編碼類型選擇合適的編碼,如果不調(diào)整會(huì)亂碼
    res = EncodingUtils.getString(buffer, "BIG5");
    //關(guān)閉
    in.close();
    }catch(Exception e){
    e.printStackTrace();
    }

  2. 從resource的asset中讀取文件數(shù)據(jù)
    String fileName = "test.txt"; //文件名字
    String res="";
    try{
    //得到資源中的asset數(shù)據(jù)流
    InputStream in = getResources().getAssets().open(fileName);
    int length = in.available();
    byte [] buffer = new byte[length];
    in.read(buffer);
    in.close();
    res = EncodingUtils.getString(buffer, "UTF-8");
    }catch(Exception e){
    e.printStackTrace();
    }

二、讀寫/data/data/<應(yīng)用程序名>目錄上的文件

//寫數(shù)據(jù)
public void writeFile(String fileName,String writestr) throws IOException{
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = writestr.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}

//讀數(shù)據(jù)
public String readFile(String fileName) throws IOException{
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/目錄下面的文件

//寫數(shù)據(jù)到SD中的文件
public void writeFileSdcardFile(String fileName,String write_str) throws IOException{
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) throws IOException{
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;
}

四、使用File類進(jìn)行文件的讀寫

//讀文件
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}

//寫文件
public void writeSDFile(String fileName, String write_str) throws IOException{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
}
參考地址:http://blog.csdn.net/ztp800201/article/details/7322110

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • java.io包里的類: InputStream/OutputStream : 以字節(jié)為單位讀寫文件內(nèi)容,一次讀/...
    buyaole閱讀 665評(píng)論 0 1
  • 1.創(chuàng)建文件夾 !/bin/sh mkdir -m 777 "%%1" 2.創(chuàng)建文件 !/bin/sh touch...
    BigJeffWang閱讀 10,465評(píng)論 3 53
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,295評(píng)論 0 17
  • 一、流的概念和作用。 流是一種有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對(duì)數(shù)據(jù)傳輸?shù)目偝苫虺橄?。即?shù)據(jù)在兩設(shè)備之間的傳輸...
    布魯斯不吐絲閱讀 10,310評(píng)論 2 95
  • 自帶雞血,就像是在升級(jí)打怪中擁有自我回血的能力一樣,不管是在什么時(shí)候總比對(duì)手擁有更多的能量,給隊(duì)友源源不斷的支援,...
    前兒閱讀 293評(píng)論 0 0

友情鏈接更多精彩內(nèi)容