当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发之文件操作模式深入理解

Android开发之文件操作模式深入理解

2019年07月24日  | 移动技术网移动技术  | 我要评论
一、基本概念
复制代码 代码如下:

// 上下文对象
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
二、组合使用
复制代码 代码如下:

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);

追加模式,但允许其他应用读写

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网