Official Android API: Stores private data in device memory.

Internal storage: The internal storage of Android is in the system, which is a file in the phone’s memory. It is not a file in an SD card (external memory). Internal storage files are stored in the data/data/files directory by default and are private (private). You can make it public, so it can be accessed by other apps. When an application is uninstalled, files in internal storage are deleted.

Use: internal storage space is very limited, mainly store the system itself and system application data, such as SharedPreferences, SQLite database, common applications should try to avoid using it.

Here are three folders:



Core Principles:

FileInputStream openFileInput(String name) and FileOutputStream(String name, int mode), The first parameter of these methods specifies the file name and the second parameter specifies the mode in which the file is opened. Specific values are as follows:

  • MODE_PRIVATE: The default operation mode, which indicates that the file is private data and can only be accessed by the application itself. In this mode, the content written to the file overwrites the content of the original file. If you want to append the new content to the original file. You can use context.mode_append instead

  • MODE_APPEND: This mode is user-friendly, it checks to see if the file exists, appends to the file if it exists, and creates a new file otherwise.

  • MODE_WORLD_READABLE: The current file can be read by other applications. !!!!!

  • MODE_WORLD_WRITEABLE: The current file can be written to other applications. !!!!!

!!!!! Official API note: Since API level 17, the constants MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE have been deprecated. Starting with Android N, using these constants will cause a SecurityException to be raised. This means that apps for Android N and later cannot share private files by name, and attempting to share the “file://” URI will cause a FileUriExposedException. If your application needs to share private files with other applications, you can use FileProvider in conjunction with FLAG_GRANT_READ_URI_PERMISSION.

Open the Data folder (which cannot be opened on a phone without root), and there will be two folders inside. (1) APP folder, which stores Apk files of all installed Apps. (2) Data folder (internal storage), including the following contents.

  • Data /data/package name/shared_prefs: Uses Sharedpreferenced to persistently store data in the local XML file.
  • Data /data/package name/Databases: database file in the App
  • Data /data/package name/files: common data
  • Data /data/package name/cache: cache file, which will be automatically deleted when the mobile phone memory is insufficient.

Android system provides the device internal storage file read and write operation API, complete file creation, content read and write (including add \ modify).

The steps for reading an internal stored private file are as follows

  • (1) Call openFilelnput(String filename), fill in the filename argument, will return a FileInputstream object.
  • (2) Read bytes using FileInputstream’s read() method.
  • (3) Call the stream’s close() method to close the stream.

Writing to an internal storage file requires obtaining an output stream of the file, writing the information to be written to the output stream as write(), and closing the stream.

The steps are as follows

  • (1) Use context. openFileOutput(string name,int mode), fill in the file name and operation mode, and obtain the FileOutputStream object.
  • (2) Write the content to be written to FileOutputStream using write().
  • (3) Use FileOutputStream close() to close the stream.

The operations for other files are as follows.

  • GetDir (String name, int mode): Gets or creates a subdirectory corresponding to name in the application’s data folder
  • File getFilesDir(): The absolute path to the File system.
  • String[] fileList(): names of all names in the internal storage path.
  • Boolean deleteFile(String name) Deletes a file named name.
  • File getDir(): Creates or opens an existing directory in the internal storage space.

The following describes the methods of the file’s main operation class

!!!!!!!!! After calling the current object’s Context:

1. Save the contents to the internal storage



Replicable code:

Public void save(String filename, String content)throws IOException{ //FileoutputStream myfos=context.openFileoutput(filename,Context.MODE_PRIVATE); File file= new File(context.getFilesDir(), filename); FileOutputStream myfos= new FileOutputStream(file); myfos.write(content.getBytes()); myfos.close(); }Copy the code

2. Obtain the content based on the file name



Replicable code:

@param filename filename * @return file content */ public String get(String filename) throws IOException { FileInputStream fis = context.openFileInput(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = -1; while ((len = fis.read(data)) ! = -1) { baos.write(data, 0, len); } return new String(baos.toByteArray()); }Copy the code

3. Add content to the end of the file



Replicable code:

*@param filename Filename *@param Content Append content */ public void append(String filename, String content) throws IOException{ FileOutputStream myfos= context.openFileOutput(filename, Context.MODE_APPEND); myfos.write(content.getBytes()); myfos.close(); }Copy the code

4. Delete files



Replicable code:

/** * delete file * @param filename filename * @return successful */ public Boolean delete(String filename) {return context.deleteFile(filename); }Copy the code
  1. Gets all file names in the internal storage path



Replicable code:

Public String [] queryAllfile(){return context.filelist (); return context.filelist (); }Copy the code

The Java file is as follows:

public class Internalstorage { private Context context; public Internalstorage(Context context) { this.context = context; } public void save(String filename, String content)throws IOException{ //FileoutputStream myfos=context.openFileoutput(filename,Context.MODE_PRIVATE); File file= new File(context.getFilesDir(), filename); FileOutputStream myfos= new FileOutputStream(file); myfos.write(content.getBytes()); myfos.close(); } /** * obtain content by filename * @param filename filename * @return file content */ public String get(String filename) throws IOException { FileInputStream fis = context.openFileInput(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = -1; while ((len = fis.read(data)) ! = -1) { baos.write(data, 0, len); } return new String(baos.toByteArray()); } @param filename filename *@param Content Append content */ public void append(String filename, String content) throws IOException{ FileOutputStream myfos= context.openFileOutput(filename, Context.MODE_APPEND); myfos.write(content.getBytes()); myfos.close(); } /** * delete file * @param filename filename * @return successful */ public Boolean delete(String filename) {return context.deleteFile(filename); } public String [] queryAllfile(){return context.filelist ();} public String [] queryAllfile(){return context. }}Copy the code