1.設置文件的讀寫權限
Int MODE的文件操作模式

Paste_Image.png
2.文件的相關操作方法

Paste_Image.png
2.1幾個比較常用的獲取系統(tǒng)路徑
- 無需向用戶申請

- 需要向用戶申請

這里講解一下
Environment.getExternalStoragePublicDirectory(String type)的用法
- 如果您需要往sdcard中保存特定類型的內容,可以考慮使用Environment.getExternalStoragePublicDirectory(String type)函數(shù),該函數(shù)可以返回特定類型的目錄,目前支持如下類型:
- DIRECTORY_ALARMS //警報的鈴聲
- DIRECTORY_DCIM //相機拍攝的圖片和視頻保存的位置
- DIRECTORY_DOWNLOADS //下載文件保存的位置
- DIRECTORY_MOVIES //電影保存的位置, 比如 通過google play下載的電影
- DIRECTORY_MUSIC //音樂保存的位置
- DIRECTORY_NOTIFICATIONS //通知音保存的位置
- DIRECTORY_PICTURES //下載的圖片保存的位置
- DIRECTORY_PODCASTS //用于保存podcast(博客)的音頻文件
- DIRECTORY_RINGTONES //保存鈴聲的位置
對應的路徑

image.png
2.2寫入文件和讀取文件
寫入文件
FileOutputStream fos=content.openFileOutput(String filename,Int mode);
fos.wirte(String content.getBytes());
fos.close;
讀取文件
FileInputStream fis=content.openFileInput(String filename);
BufferedReader br=new BufferedReader(InputStreamReader(fis));
string content=br.readline();
3.讀取SD卡上的文件

Paste_Image.png
這里讀寫數(shù)據(jù)用如下
FileOutputStream output = new FileOutputStream(filename);
output.write(filecontent.getBytes());
//將String字符串以字節(jié)流的形式寫入到輸出流中
output.close();
StringBuilder sb = new StringBuilder("");
FileInoutStream fis=new FileInputStream(filename);
int len=0;
byte[]bytes=new byte[1024];
while((len=fis.read(bytes)=)!=-1){
sb.append(new String(temp, 0, len))
}
這里不能再用openFileInput,那個是往手機內存中讀寫數(shù)據(jù)的