Android開發4——文件操作模式
一、基本概念
// 上下文對象 private Context context; public FileService(Context context) { super(); this.context = context; } // 保存文件方法 public void save(String filename, String fileContent) throws Exception { FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE); fos.write(fileContent.getBytes("UTF-8")); fos.close(); }
私有模式
①隻能被創建這個文件的當前應用訪問
②若文件不存在會創建文件;若創建的文件已存在則會覆蓋掉原來的文件
Context.MODE_PRIVATE = 0;
追加模式
①私有的
②若文件不存在會創建文件;若文件存在則在文件的末尾進行追加內容
Context.MODE_APPEND = 32768;
可讀模式
①創建出來的文件可以被其他應用所讀取
Context.MODE_WORLD_READABLE=1;
可寫模式
①允許其他應用對其進行寫入。
Context.MODE_WORLD_WRITEABLE=2
以上文件操作模式均針對保存在手機自帶存儲空間的文件。若文件存儲在SDCard上,則不受讀寫控製。
二、組合使用
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
允許其他應用讀寫,並默認覆蓋
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_APPEND+Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
追加模式,但允許其他應用讀寫
參考地址:https://www.eoeandroid.com/thread-68561-1-1.html
最後更新:2017-04-03 20:19:50