关于android data/data/包名 目录

来源:互联网 发布:网络龙虎概率分析软件 编辑:程序博客网 时间:2024/06/05 01:54

关于data/data/包名 目录

1.关于此目录

此目录是app的私有目录,本身可以访问,外部程序没有访问权限,一些不想让用户看到的文件可以往这里边扔;此目录在程序卸载后将被删除;对应设置中的“数据”一项,如果清空数据,这个目录将会被清空。app自身对此目录的读写不需要额外的权限,because this path is internal storage;

2.目录的操作

Context 提供了一些API方便我们操作此目录

1. getFilesDir();
此函数返回的路径:/data/data/xxx包名/files
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

在这个目录下创建文件有函数:
1.1.public FileOutputStream openFileOutput(String name, int mode)
Open a private file associated with this Context’s application package for writing. Creates the file if it doesn’t already exist.
值得一提的是其中第二个参数 mode,自从api17之后,MODE_WORLD_WRITEABLEMODE_WORLD_READABLE出于安全性考虑被废了,所以改变此目录下文件的权限用这种方法是不行了;
注意,此函数只能在files目录下创建文件,而不能创建目录,name参数不能含有path separators

相对应的读取方法:
1.2.public FileInputStream openFileInput(String name)
1.1和1.2这两个函数都是操作/data/data/xxx包名/files目录下的文件,其中参数@param name The name of the file to open; can not contain path separators.
相应源码:

if (name.indexOf(File.separatorChar) < 0) {    return new File(base, name);}throw new IllegalArgumentException("File " + name + " contains a path separator");

2. public File getDir(String name, int mode);
此函数返回/data/data/xxx包名/目录下名为app_name名称的文件夹的路径,如果不存在,则创建,其种name就是函数的第一个参数;mode的说明同上。

name = "app_" + name;File file = makeFilename(getDataDirFile(), name);

一些常见函数的说明:
1. 关于函数public boolean isDirectory();
如果file不存在,则返回值是false,所以先调动函数public boolean exists(),判断是否存在,之后调用isDirectory();才有意义;
2. public boolean mkdirs();
函数说明:
Creates the directory named by this file, creating missing parent directories if necessary. Use mkdir() if you don't want to create missing parents.
如果父路径不存在,可以创建;应该注意这个函数创建的是 路径,而不是文件,即使你最后file.xxx,也只会创建file.xxx的文件夹

0 0
原创粉丝点击