跟我學Android之十二 檔案解析與處理

來源:互聯網
上載者:User

標籤:

本章內容


第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_CODECONTEXT_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>/filesu返回值是一個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();


跟我學Android之十二 檔案解析與處理

聯繫我們

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