This is the 26th day of my participation in the August Text Challenge.More challenges in August

introduce

In most cases our APP creates files that no other APP needs to access. The system provides the following locations for storing application-specific files:

  • Internal storage space directories: These directories include both exclusive locations for storing persistent files and other locations for storing cached data. The system prevents other apps from accessing these locations, and in Android 10 (API level 29) and later, these locations are encrypted. These characteristics make these locations ideal for storing sensitive data that only the application itself can access.
  • External storage space directoryThese directories include both exclusive locations for storing persistent files and other locations for storing cached data. While other applications can access these directories with appropriate permissions, the files stored in these directories are for your application’s use only. If you explicitly intend to create files that other applications can access, your application should instead store these files in shared storage in external storage spaceMediaStorePart.

Note: If an application is uninstalled, the system will remove the files saved in the application-specific storage space.

Access to internal storage space

For each application, the system provides a directory in the internal storage space where the application can organize its files. One directory is designed for the application’s persistent files, while the other contains the application’s cache files. You do not need any system permissions to read and write files in these directories.

Other applications cannot access files stored in the internal storage space. This makes the internal storage space suitable for storing application data that should not be accessed by other applications. Before writing application data, you need to query the available space on the device.

private long queryMemory(a){
    long availableBytes = 0;
    StorageManager storageManager = getApplicationContext().getSystemService(StorageManager.class);
    UUID appSpecificInternalDirUuid = null;
    try {
        appSpecificInternalDirUuid = storageManager.getUuidForPath(getFilesDir());
        availableBytes = storageManager.getAllocatableBytes(appSpecificInternalDirUuid);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return availableBytes;
}
Copy the code

Accessing persistent files

Access and store files

File file = new File(context.getFilesDir(), filename);
Copy the code

Use information flow to store files

In addition to using the File API, you can also call openFileOutput() to get FileOutputStream that will be written to files in the filesDir directory.

String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = this.openFileOutput(filename, this.MODE_PRIVATE)) {
    fos.write(fileContents.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
    e.printStackTrace();
}
Copy the code

Use information flow to access files

String filename = "myfile"; 
FileInputStream fis = context.openFileInput(filename);
InputStreamReader inputStreamReader =
        new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
    String line = reader.readLine();
    while(line ! =null) {
        stringBuilder.append(line).append('\n'); line = reader.readLine(); }}catch (IOException e) {
    // Error occurred when opening raw file for reading.
} finally {
    String contents = stringBuilder.toString();
}
Copy the code

Viewing the File List

Call fileList() to get an array containing the names of all files in the filesDir directory

Array<String> files = context.fileList();
Copy the code

Create a nested directory

File directory = context.getFilesDir();
File file = new File(directory, filename);
Copy the code

Create a cache file

This cache directory is intended to store a small amount of sensitive data for your application. To determine how much cache space is currently available for your application, call getCacheQuotaBytes().

create

File.createTempFile(filename, null, context.getCacheDir());
Copy the code

access

// When the internal storage space of the device is insufficient, Android may delete these cache files to reclaim space. Therefore, you need to check whether the cache file exists before reading.
File cacheFile = new File(context.getCacheDir(), filename);
Copy the code

Removing cached files

context.deleteFile(cacheFileName);
Copy the code