視頻課:https://edu.csdn.net/course/play/7621
本章內容
第1節 File Explorer操作
第2節 SharedPreferences
第3節 普通文件操作
第4節 SD卡讀寫操作
本章目標
熟練掌握 FileExplorer 的用法
熟練掌握 SharedPreference 文件操作。
熟練掌握普通文件的讀寫操作。
熟練掌握 SD 卡讀寫操作的方法。
FileExplorer操作
查看文件結構
創建文件夾
導 入文件
導出文件及文件夾
刪除文件
SharedPreferences概述
SharedPreferences主要用於保存類似配置信息的內容,SharedPreferences以XML格式保存數據,保存在/data/data/<package>/shared_prefs目錄中,跟Properties中的信息類似,主要是鍵值對
讀取SharedPreferences
首先通過Context. getSharedPreferences方法獲得對象
第一個參數是文件名,需要包含後綴名(自動設置爲xml)
第二個參數是訪問模式,和普通文件的訪問模式相同
通過SharedPreferences中的方法讀取數據
SharedPreferences sp = getSharedPreferences("config",
Context.MODE_PRIVATE);
Stringusername = sp.getString(“username”,“root”);
Stringpassword = sp.getString(“password”,“123456”);
寫入SharedPreferences
首先通過Context. getSharedPreferences方法獲得對象
獲取對象時採用寫模式打開
通過SharedPreferences獲得Editor對象
Editor對象中包含了寫數據的方法
數據寫入完成後一定要執行commit方法讓數據生效
SharedPreferences sp = getSharedPreferences("config",
Context.MODE_PRIVATE);
Editor editor =sp.edit();
editor.put(“username”, “root”);
editor.put(“password”, “123456”);
editor.commit();
pe:solid;mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%'>方法讓數據生效
外部訪問SharedPreferences
在一個應用中訪問另一個應用的SharedPreferences數據
通過Context. createPackageContext 創建Context的實例
第一個參數是另一個應用的package名
第二個參數是選項標誌
CONTEXT_INCLUDE_CODE
CONTEXT_IGNORE_SECURITY
通過建立的Context對象訪問SharedPreferences
Contextcontext = createPackageContext(PKGNAME,
CONTEXT_IGNORE_SECURITY);
SharedPreferences cfg = context.getSharedPreferences(
PREFERENCE_NAME, MODE);
在一個應用中訪問另一個應用的SharedPreferences數據
u關於權限的幾個注意點
Ø兩個應用的android:sharedUserId的內容要相同
Ø雙方使用MODE_WORLD_READABLE或MODE_WORLD_WRITEABLE模式讀寫內容
利用openFileInput讀取文件
利用openFileInput讀取文件
u這是Context中的一個方法
Ø能夠從應用相關的路徑中打開一個文件輸入流
u文件位置
Ø/data/data/<package>/files
u返回值是一個FileInputStream的對象
Ø這是一個文件輸入字節流
利用openFileInput讀取文件
u讀取文件的一個示例
FileInputStream inputStream = this.openFileInput(fileName);
byte[]bytes = new byte[1024];
ByteArrayOutputStream os= new ByteArrayOutputStream();
while(inputStream.read(bytes)!= -1) {
os.write(bytes, 0, bytes.length);
}
inputStream.close();
os.close();
Stringcontent = new String(os.toByteArray());
showTextView.setText(content);
利用openFileOutput方法寫入文件
u文件不存在時自動創建
u方法的第二個參數爲打開模式
ØMODE_PRIVATE只能創建它的應用訪問,重複寫入時會文件覆蓋
ØMODE_APPEND 私有訪問,重複寫入時會在文件的末尾進行追加
ØMODE_WORLD_READABLE公用模式,可讀
ØMODE_WORLD_WRITEABLE公用模式,可讀寫
u通常建議使用私有模式
Ø公用模式下操作文件很危險,因爲一旦併發將會帶來程序的漏洞
利用openFileOutput方法寫入文件
u寫入文件示例
FileOutputStream outputStream = openFileOutput(fileName,
Activity.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
合理利用緩存
u文件的讀寫是對存儲設備的訪問
Ø輸入輸出的速率比較低
Ø頻繁訪問時會影響性能
u適當使用緩存提交效率
Ø將文件中需要頻繁訪問的內容讀入內存
Ø在內存中進行數據的操作
Ø定期或者需要時再寫入文件
Ø減少文件的輸入輸出次數
u但是緩存不能太大,以免佔用太多資源導致系統性能下降
瀏覽SD卡上的文件
uSD卡通常是手機的擴展存儲
u掛載到手機操作系統的文件系統下/mnt/sdcard/
uSD卡上的目錄對所有應用都是可寫的
u使用File類瀏覽SD卡上的目錄內容
mso-color-index:1;mso-font-kerning:12.0pt;language:zh-CN;mso-style-textfill-type:solid;mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha:100.0%'>公用模式下操作文件很危險,因爲一旦併發將會帶來程序的漏洞
Filedir = new File(“/mnt/sdcard”);
String[]s = dir.list();
讀取SD卡上的文件
u使用FileInputStream或者FileReader進行文件讀取
FileInputStream in = new FileInputStream(“/mnt/sdcard/……”);byte[] bytes = new byte[1024];ByteArrayOutputStream os= new ByteArrayOutputStream();while (in.read(bytes) != -1) {os.write(bytes, 0, bytes.length);}in.close();os.close();String content = new String(os.toByteArray());showTextView.setText(content);
將文件保存到SD卡上
u使用FileOutputStream或者FileWriter進行文件寫入
FileOutputStream out = new FileOutputStream(“/mnt/sdcard/..”);out.write(content.getBytes());out.flush();out.close();