一、讀取assets目錄下的文件
try {
InputStream is = getResources().getAssets().open("asset.txt");
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String str_asset = "";
while ((str_asset = br.readLine()) != null) {
Log.d("MainActivity", str_asset);
}
} catch (IOException e) {
e.printStackTrace();
}
二、讀取raw目錄下的文件
try {
InputStream is = getResources().openRawResource(R.raw.raw);
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String str_raw = "";
while ((str_raw = br.readLine()) != null) {
Log.d("MainActivity", str_raw);
}
} catch (IOException e) {
e.printStackTrace();
}
三、讀取手機(jī)存儲(chǔ)文件(內(nèi)置)
try {
FileInputStream fis = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
char input[] = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String readed = new String(input);
show_text.setText(readed);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
四、寫入到手機(jī)存儲(chǔ)(內(nèi)置)
try {
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
osw.write(mText.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
Toast.makeText(MainActivity.this, "寫入完成", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
五、讀取SDCARD存儲(chǔ)文件
File myfile = new File(sdcard, "This is my file.txt");
if (myfile.exists()) {
try {
FileInputStream fis = new FileInputStream(myfile);
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
char input[] = new char[fis.available()];
isr.read(input);
String str = new String(input);
show_text_out.setText(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
六、寫入SDCARD存儲(chǔ)
File myFile = new File(sdcard, "This is my file.txt");
if (!sdcard.exists()) {
Toast.makeText(MainActivity.this, "當(dāng)前系統(tǒng)不具備SD卡目錄", Toast.LENGTH_SHORT).show();
return;
}
try {
myFile.createNewFile();
Toast.makeText(MainActivity.this, "文件創(chuàng)建完成", Toast.LENGTH_SHORT).show();
FileOutputStream fos = new FileOutputStream(myFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
osw.write(text_out.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
Toast.makeText(MainActivity.this, "文件已經(jīng)寫入完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Github地址:https://github.com/wuyinlei/AndroidFileOperator
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。