Android—文件进行数据存储(四中操作模式)

来源:互联网 发布:python metrics模块 编辑:程序博客网 时间:2024/05/01 11:54

简介:

    Android进行文件存储主要有四种操作模式

    1》Context.MODE_PRIVATE:默认模式,私有模式只能被本身应用进行访问,其它应用不能进行访问,本身应用访问文件,如果进行操作会对文件进行覆盖。

    2》Context.MODE_APPEND:append模式下对文件访问会自动检查文件是否存在,会追加内容,如果文件不存在则进行创建文件。

    3》MODE_WORLD_WRITEABLE:表示可以被其它应用对data\data\下的文件进行写,但不能进行读。

    4》MODE_WORLD_READABLE:表示可以被其它应用对data\data\下的文件进行读,但不能进行写。

    如何想读写:MODE_WORLD_WRITEABLE+MODE_WORLD_READEABLE


代码来了解四种操作模式:

提取方法:

private void fileWrite(String content, FileOutputStream fos)throws IOException {fos.write(content.getBytes());fos.close();}

工具类:

public class IOUtil {public static String read(File file) throws Exception{FileInputStream fis=new FileInputStream(file);ByteArrayOutputStream outStream=new ByteArrayOutputStream();byte[] buff=new byte[1024];int len=0;while((len=fis.read(buff))!=-1){outStream.write(buff, 0, len);}fis.close();outStream.close();return new String(outStream.toByteArray());}}


1》我们先了解下Contex.MODE_PRIVATE

// 私有操作模式,創建出來的文件只能被本應由訪問,其它应由无法访问,写入文件的内容// 会覆盖原文件的内容FileOutputStream fos = context.openFileOutput(filename,Context.MODE_PRIVATE);fileWrite(content, fos);
本身应用时可以访问的,其它应用不能访问。

2》Context.MODE_APPEND

File项目下:                   // 私有操作模式,創建出來的文件只能被本應由訪問,其它应由无法访问,写入文件的内容// 会覆盖原文件的内容FileOutputStream fos = context.openFileOutput(filename,Context.MODE_APPEND);fileWrite(content, fos);
Other项目下:        String path="/data/data/com.example.file/files/Append.txt";        File file=new File(path);        String content=IOUtil.read(file);        Log.i(TAG,content);


抛出异常:java.io.FileNotFoundException:/data/data/com.example.file/files/Append.txt(Permission denied)

很明显权限不够

android有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给他一个userid,当该应用要去访问其它资源比如文件的时候,就需要userid匹配.


3》MODE_WORLD_WRITEABLE

File项目下        public void testWorld_writeable() throws Exception {FileService service = new FileService(this.getContext());service.saveWorld_writeable("World_writeable.txt","www.World_writeable.net");}


Other项目下       public void readWrite() throws Exception {String path="/data/data/com.example.file/files/World_writeable.txt";File file=new File(path);FileOutputStream fos=new FileOutputStream(file);fos.write("itcast".getBytes());fos.close();}

4》MODE_WORLD_READABLE

File项目下       public void saveWorld_readable(String filename, String content)throws Exception {// 私有操作模式,創建出來的文件只能被本應由訪問,其它应由无法访问,写入文件的内容// 会覆盖原文件的内容FileOutputStream fos = context.openFileOutput(filename,Context.MODE_WORLD_READABLE);fileWrite(content, fos);}

Other项目下public void readRead() throws Exception {String path="/data/data/com.example.file/files/World_readable.txt";File file=new File(path);String content=IOUtil.read(file);Log.i(TAG,content);}


可读可写:

MODE_WORLD_WRITEABLE+MODE_WORLD_READEABLE

File项目下       public void saveReadAndWrite(String filename, String content)throws Exception {FileOutputStream fos = context.openFileOutput(filename,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);fileWrite(content, fos);}

Other项目下        public void readWriteRead() throws Exception {String path="/data/data/com.example.file/files/writeRead.txt";File file=new File(path);String content=IOUtil.read(file);Log.i(TAG,content);FileOutputStream fos=new FileOutputStream(file);fos.write("itcast".getBytes());fos.close();}








原创粉丝点击