一、基本概念
復(fù)制代碼 代碼如下:
// 上下文對(duì)象
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();
}
私有模式
①只能被創(chuàng)建這個(gè)文件的當(dāng)前應(yīng)用訪問
②若文件不存在會(huì)創(chuàng)建文件;若創(chuàng)建的文件已存在則會(huì)覆蓋掉原來的文件
Context.MODE_PRIVATE = 0;
追加模式
①私有的
②若文件不存在會(huì)創(chuàng)建文件;若文件存在則在文件的末尾進(jìn)行追加內(nèi)容
Context.MODE_APPEND = 32768;
可讀模式
①創(chuàng)建出來的文件可以被其他應(yīng)用所讀取
Context.MODE_WORLD_READABLE=1;
可寫模式
①允許其他應(yīng)用對(duì)其進(jìn)行寫入。
Context.MODE_WORLD_WRITEABLE=2
二、組合使用
復(fù)制代碼 代碼如下:
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
允許其他應(yīng)用讀寫,并默認(rèn)覆蓋
復(fù)制代碼 代碼如下:
FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_APPEND+Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
追加模式,但允許其他應(yīng)用讀寫