小結Android檔案私人儲存及資源檔的存取

來源:互聯網
上載者:User
Android檔案私人儲存


首先內部儲存路徑為/data/data/youPackageName/,下面講解的各路徑都是基於你自己的應用的內部儲存路徑下。所有內部儲存中儲存的檔案在使用者卸載應用的時候會被刪除。

一、 files

1. Context.getFilesDir(),該方法返回/data/data/youPackageName/files的File對象。
2. Context.openFileInput()與Context.openFileOutput(),只能讀取和寫入files下的檔案,返回的是FileInputStream和FileOutputStream對象。
3. Context.fileList(),返回files下所有的檔案名稱,返回的是String[]對象。
4. Context.deleteFile(String),刪除files下指定名稱的檔案。

二、cache

Context.getCacheDir(),該方法返回/data/data/youPackageName/cache的File對象。

三、getDir

getDir(String name, int mode),返回/data/data/youPackageName/下的指定名稱的檔案夾File對象,如果該檔案夾不存在則用指定名稱建立一個新的檔案夾。

Android私人檔案資源檔的存取


Android有一套自己的安全模型, 可參見Android開發文檔。當應用程式安裝時就會分配一個userid,當應用要去訪問其他資源比如檔案時,需要userid匹配。預設情況下 ,任何應用建立的檔案,資料庫, sharedpreferences都應該是私人的(位於/data/data/your_project/files/),其餘程式無法訪問。除非在建立時指明是MODE_WORLD_READABLE 或者 MODE_WORLD_WRITEABLE,只要這樣其餘程式才能正確訪問。因為有這種Android讀寫檔案的方法在安全上有所保障,進程開啟檔案時Android要求檢查進程的user id。所以不能直接用java的api來開啟,因為java的io函數沒有提這個機制 。有一個問題系統必須已經root,並且檔案瀏覽器取得root許可權否則通過檔案瀏覽器是看不到那些目錄的。

一、私人檔案夾下的檔案存取(./data/data/com.包名/files/下面,相當於程式工作目錄)

無法用java的api直接開啟程式私人的資料 ,預設路徑為/data/data/your_project/files/,從sdcard中去讀檔案,首先要把檔案通過android-sdk-windowstoolsadb.exe把本機電腦上的檔案copy到sdcard上去,adb.exe push e:/Android.txt/sdcard/, 不可以用adb.exe push e:Android.txt sdcard,注意斜杠的,在linux下和winodws下是不一樣的, 同樣: 把模擬器上的檔案copy到本機電腦上用: adb pull ./sdcard/Android.txt e:/ ,讀取路徑可以用“/sdcard/Android.txt”也可以用“mnt/sdcard/Android.txt”

如:

FileReader file = new FileReader("Android.txt");

這裡特彆強調私人資料!言外之意是如果某個檔案或者資料不是程式私人的,既訪問它時無須經過Android的許可權檢查,那麼還是可以用java的io api來直接存取的。所謂的非私人資料是只放在sdcard上的檔案或者資料,

可以用java的io api來直接開啟sdcard上檔案。

 如:

FileReader file = new FileReader("/sdcard/Android.txt");

如果要開啟程式自己私人的檔案和資料,那必須使用Activity提供openFileOutput和openFileInput方法。建立程式私人的檔案,由於許可權方面的要求,必須使用activity提供的Android讀寫檔案方法。
如:

 代碼如下 複製代碼
 import java.io.FileInputStream;   
 import java.io.FileOutputStream;   
 import org.apache.http.util.EncodingUtils;   
    
 public void writeFileData(String fileName,String message){   
     try{   
      FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE); 
      //OutputStreamWriter outWriter = new OutputStreamWriter(fout);  
      byte [] bytes = message.getBytes();   
      fout.write(bytes);   
       fout.close();   
      }   
     catch(Exception e){   
      e.printStackTrace();   
     }   
 }      
    
 public String readFileData(String fileName){   
      String res="";   
      try{   
       //FileInputStream fin = openFileInput(fileName); 
       //InputStreamReader inReader = new InputStreamReader(fin); 
       //或者 
       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;   
     }



二、從resource中的raw檔案夾中擷取檔案並讀取資料(資源檔只能讀不能寫,在"程式名resrawFileName.txt")

 代碼如下 複製代碼
public String getFromRaw(String fileName){   
    String res = "";   
       try{   
        InputStream in = getResources().openRawResource(R.raw.test1);    
        //資源在Testresrawtest1.txt 
        int length = in.available();         
        byte [] buffer = new byte[length];          
        in.read(buffer);   
        //依bbi.txt的編碼類別型選擇合適的編碼,如果不調整會亂碼         
        res = EncodingUtils.getString(buffer, "UTF-8");  
        //res =EncodingUtils.getString(buffer, "BIG5"); 
        //res =EncodingUtils.getString(buffer, "UNICODE"); 
        in.close();              
       }   
       catch(Exception e){   
        e.printStackTrace();           
       }   
    return res ;   
   }


三、從asset中擷取檔案並讀取資料(資源檔只能讀不能寫,在"程式名assertsFileName.txt")

 代碼如下 複製代碼
public String getFromAsset(String fileName){   
    String res="";   
    try{   
     InputStream in = getResources().getAssets().open(fileName);     
     int length = in.available();           
        byte [] buffer = new byte[length];          
        in.read(buffer);              
        res = EncodingUtils.getString(buffer, "UTF-8");       
    }   
    catch(Exception e){   
     e.printStackTrace();           
    }   
    return res;   
   } 

聯繫我們

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