Android中的檔案儲存體資料方式

來源:互聯網
上載者:User

 

1.檔案儲存體資料使用了Java中的IO操作來進行檔案的儲存和讀取,只不過Android在Context類中封裝好了輸入資料流和輸出資料流的擷取方法。
建立的隱藏檔儲存在/data/data/<package name>/files檔案夾下。

2.操作。
儲存檔案內容:通過Context.openFileOutput擷取輸出資料流,參數分別為檔案名稱和儲存模式。
讀取檔案內容:通過Context.openFileInput擷取輸入資料流,參數為檔案名稱。
刪除檔案:Context.deleteFile刪除指定的檔案,參數為將要刪除的檔案的名稱。
擷取檔案名稱列表:通過Context.fileList擷取files目錄下的所有檔案名稱數組。
*擷取檔案路徑的方法:
絕對路徑:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以擷取到”/data/data/<package name>/files”

3.四種檔案儲存的模式。
Context.MODE_PRIVATE 為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下寫入的內容會覆蓋原檔案的內容。
Context.MODE_APPEND 檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。
MODE_WORLD_READABLE 表示當前檔案可以被其他應用讀取。
MODE_WORLD_WRITEABLE 表示當前檔案可以被其他應用寫入。
在使用模式時,可以用”+”來選擇多種模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);

下面通過程式來示範下檔案儲存體的使用。完整代碼下載:

點選連結下載android_files
大小 : 44.29 kB
下載次數 : 0
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
/**  * MainActivity  *   * @author zuolongsnail  *   */   public class MainActivity extends Activity {   private EditText writeET;   private Button writeBtn;   private TextView contentView;   public static final String FILENAME = "setting.set";    @Override   public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   writeET = (EditText) findViewById(R.id.write_et);   writeBtn = (Button) findViewById(R.id.write_btn);   contentView = (TextView) findViewById(R.id.contentview);   writeBtn.setOnClickListener(new OperateOnClickListener());   }    class OperateOnClickListener implements OnClickListener {   @Override   public void onClick(View v) {   writeFiles(writeET.getText().toString());   contentView.setText(readFiles());   System.out.println(getFilesDir());   }   }    // 儲存檔案內容   private void writeFiles(String content) {   try {   // 開啟檔案擷取輸出資料流,檔案不存在則自動建立   FileOutputStream fos = openFileOutput(FILENAME,   Context.MODE_PRIVATE);   fos.write(content.getBytes());   fos.close();   } catch (Exception e) {   e.printStackTrace();   }   }    // 讀取檔案內容   private String readFiles() {   String content = null;   try {   FileInputStream fis = openFileInput(FILENAME);   ByteArrayOutputStream baos = new ByteArrayOutputStream();   byte[] buffer = new byte[1024];   int len = 0;   while ((len = fis.read(buffer)) != -1) {   baos.write(buffer, 0, len);   }   content = baos.toString();   fis.close();   baos.close();   } catch (Exception e) {   e.printStackTrace();   }   return content;   }   }  

程式:

提供一個檔案儲存體資料的工具類:

123456789101112131415161718192021222324252627282930313233343536373839404142434445
/**  * 檔案儲存體資料方式工具類  *   * @author zuolongsnail  */   public class FilesUtil {   /**  * 儲存檔案內容  *   * @param c  * @param fileName  *            檔案名稱  * @param content  *            內容  */   private void writeFiles(Context c, String fileName, String content, int mode)   throws Exception {   // 開啟檔案擷取輸出資料流,檔案不存在則自動建立   FileOutputStream fos = c.openFileOutput(fileName, mode);   fos.write(content.getBytes());   fos.close();   }    /**  * 讀取檔案內容  *   * @param c  * @param fileName  *            檔案名稱  * @return 返迴文件內容  */   private String readFiles(Context c, String fileName) throws Exception {   ByteArrayOutputStream baos = new ByteArrayOutputStream();   FileInputStream fis = c.openFileInput(fileName);   byte[] buffer = new byte[1024];   int len = 0;   while ((len = fis.read(buffer)) != -1) {   baos.write(buffer, 0, len);   }   String content = baos.toString();   fis.close();   baos.close();   return content;   }   }  
相關文章

聯繫我們

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