当前位置:   article > 正文

14、Android 文件存储_android开发安卓14读取文件

android开发安卓14读取文件

数据存储在我们android开发中是不可避免的,而且,我们也都知道数据存储的方式,文件存储,SharedPreference,数据库存储等。但是应该也有一部分人, 只是知道这些存储方式,或者说只知道怎么用,但是不知道具体被保存在什么地方。本篇文章将详细分析这三种存储方式。
算了不卖关子了,其实,在我看来数据存储,或者说数据的持久化,就分为两类。内部存储,外部存储。

内部存储空间
路径:data/data/包名
1 、getFileDir()
路径:data/data/包名/file

/**
 * Returns the absolute path to the directory on the filesystem where files
 * created with {@link #openFileOutput} are stored.
 * <p>
 * The returned path may change over time if the calling app is moved to an
 * adopted storage device, so only relative paths should be persisted.
 * <p>
 * No additional permissions are required for the calling app to read or
 * write files under the returned path.
 *
 * @return The path of the directory holding application files.
 * @see #openFileOutput
 * @see #getFileStreamPath
 * @see #getDir
 */
public abstract File getFilesDir();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

通过官方注释我们可以看到,这个方法返回一个绝对路径,这个绝对路径是有openFilePutput()方法创建的文件。重要的是最后一句,当在这个路径下调用程序来进行读写操作的时候,是不需要任何额外的权限的。也就是说,我们在使用内部存储的方法存储数据的时候,不需要用在manifest文件中声明权限,也不需要考虑android6.0的运行时权限的。

1.1 openFileOutput()
根据上面的介绍我们知道了,这个方法是用来创建一个内部存储的文件的。

/**
 * Open a private file associated with this Context's application package
 * for writing. Creates the file if it doesn't already exist.
 * <p>
 * No additional permissions are required for the calling app to read or
 * write the returned file.
 *
 * @param name The name of the file to open; can not contain path
 *            separators.
 * @param mode Operating mode.
 * @return The resulting {@link FileOutputStream}.
 * @see #MODE_APPEND
 * @see #MODE_PRIVATE
 * @see #openFileInput
 * @see #fileList
 * @see #deleteFile
 * @see java.io.FileOutputStream#FileOutputStream(String)
 */
public abstract FileOutputStream openFileOutput(String name, @FileMode int mode)
    throws FileNotFoundException;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

再看官方的注释,打开一个和应用程序包名相关联的私有文件来写入。如果文件不存在就会创建这个文件。并且又说明了进行读写操作的时候是不用任何权限的。

这个方法需要传两个参数,第一个参数就是你想要创建文件的文件名,注意不能包含”/”斜杠符号。
第二个参数是操作模式。官方给我们了两种模式选择:

/**
 * File creation mode: for use with {@link #openFileOutput}, if the file
 * already exists then write data to the end of the existing file
 * instead of erasing it.
 * @see #openFileOutput
 * 文件创建模式,在openFileOuput方法中使用,如果文件存在,那么会在已存在的文件后面接着写入数据,而不是删除已存在的数据。

 */
public static final int MODE_APPEND = 0x8000; /**
 * File creation mode: the default mode, where the created file can only
 * be accessed by the calling application (or all applications sharing the
 * same user ID).
 * 文件创建模式,默认的模式,用这个模式创建的文件只能被当前调用的应用程序访问。(或者所有拥有相同UID的应用,
 * 这个UID其实就是每个进程的UID,也就是说同一进程访问,这里涉及到多进程的知识,在此不详细展开了)
 */
public static final int MODE_PRIVATE = 0x0000;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.2 openFileInput()

/**
 * Open a private file associated with this Context's application package
 * for reading.
 *
 * @param name The name of the file to open; can not contain path
 *             separators.
 *
 * @return The resulting {@link FileInputStream}.
 *
 * @see #openFileOutput
 * @see #fileList
 * @see #deleteFile
 * @see java.io.FileInputStream#FileInputStream(String)
 */
public abstract FileInputStream openFileInput(String name)
    throws FileNotFoundException;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

官方注释,打开一个和程序包名相关联的私有文件来读取。这个方法只有一个参数,就是要打开的文件名,这个文件名也不能包含“/”斜杠符号。
1.3 其他API
下面连个方法就不详细介绍了,一个是删除内部存储文件,一个是获取内部存储文件的列表方法。(只能操作file文件夹下的文件)

/**
 * Delete the given private file associated with this Context's
 * application package.
 *
 * @param name The name of the file to delete; can not contain path
 *             separators.
 *
 * @return {@code true} if the file was successfully deleted; else
 *         {@code false}.
 *
 * @see #openFileInput
 * @see #openFileOutput
 * @see #fileList
 * @see java.io.File#delete()
 */
public abstract boolean deleteFile(String name); /**
 * Returns an array of strings naming the private files associated with
 * this Context's application package.
 *
 * @return Array of strings naming the private files.
 *
 * @see #openFileInput
 * @see #openFileOutput
 * @see #deleteFile
 */
public abstract String[] fileList();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2、getCacheDir()
路径:data/data/包名/cache
获取内部存储空间的缓存路径。他并没有类似openFileOutput和openFileInput的方法也没偶遇delete和fileList的方法。所以如果要往这个文件夹下写入文件就要用一般用到的File file = new File(); 先创建一个文件,然后再利用FileOutputStream往文件里写入数据。这个好像用的挺少的,可能是因为当系统内存不足时,会把他整个目录删掉吧。
3、getDir()

/**
 * Retrieve, creating if needed, a new directory in which the application
 * can place its own custom data files.  You can use the returned File
 * object to create and access files in this directory.  Note that files
 * created through a File object will only be accessible by your own
 * application; you can only set the mode of the entire directory, not
 * of individual files.
 * <p>
 * The returned path may change over time if the calling app is moved to an
 * adopted storage device, so only relative paths should be persisted.
 * <p>
 * Apps require no extra permissions to read or write to the returned path,
 * since this path lives in their private storage.
 *
 * @param name Name of the directory to retrieve.  This is a directory
 * that is created as part of your application data.
 * @param mode Operating mode.
 *
 * @return A {@link File} object for the requested directory.  The directory
 * will have been created if it does not already exist.
 *
 * @see #openFileOutput(String, int)
 */
public abstract File getDir(String name, @FileMode int mode);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

小结
1、内部存储空间的路径为data/data/包名
2、内部存储空间只有file文件夹下的读,写,删,操作系统给我们提供了。
3、内部存储空间的文件都是只能本程序访问,其他程序没有权限访问。
4、内部存储空间的文件 在应用被卸载的时候会被一并删除,更新的时候不会。
5、访问内部存储空间,并不需要任何的权限。
6、内部存储空间其实就是手机的内存,所以不能往这里面存入太大的文件,不然手机没有内存就无法正常使用了。
7、cache与files的差别在于,如果手机的内部存储空间不够了,会自行选择cache目录进行删除,因此,不要把重要的文件放在cache文件里面,可以放置在files里面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/902606
推荐阅读
相关标签
  

闽ICP备14008679号