Data storage and access
Many times our software needs to store or access the processed data again.Android provides a variety of ways to store data, including the following:
document
SharedPreferences
SQLite database
Content provider
network
Use files for data storage
First, let me introduce to you how to store data using files. Activity provides the openFileOutput() method that can be used to output data to a file. The specific implementation process is the same as storing data to a file in a J2SE environment.
public class FileActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
...
FileOutputStream outStream = ("", Context.MODE_PRIVATE);
("CSDN".getBytes());
();
}
}
The first parameter of the openFileOutput() method is used to specify the file name and cannot contain the path separator "/". If the file does not exist, Android will automatically create it. The created file is saved in the /data/data/<package name>/files directory, such as: /data/data//files/ . By clicking on the Eclipse menu "Window" - "Show View" - "Other", expand the android folder in the dialog window, select the File Explorer view below, and then expand the /data/data/<package name>/files directory in the File Explorer view to see the file.
The second parameter of the openFileOutput() method is used to specify the operation mode. There are four modes, namely: Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
Context.MODE_PRIVATE: It is the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file. If you want to append the newly written content to the original file. Context.MODE_APPEND can be used
Context.MODE_APPEND: The mode will check whether the file exists. If it exists, it will add content to the file, otherwise a new file will be created.
Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE are used to control whether other applications have permission to read and write the file.
MODE_WORLD_READABLE: means that the current file can be read by other applications; MODE_WORLD_WRITEABLE: means that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass it in:
openFileOutput("", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
Android has its own security model. When the application (.apk) is installed, the system will assign it a userid. When the application wants to access other resources such as files, the userid needs to match it. By default, any file created by the application, sharedpreferences, and databases should be private (located in /data/data/<package name>/files) and cannot be accessed by other programs. Unless Context.MODE_WORLD_READABLE or Context.MODE_WORLD_WRITEABLE is specified at creation time, only in this way other programs can access it correctly.
Read file content
If you want to open a file stored in the /data/data/<package name>/files directory application privately, you can use Activity to provide the openFileInput() method.
FileInputStream inStream = ().openFileInput("");
("FileTest", readInStream(inStream));
Please refer to the note below this page for the readInStream() method.
Or use the absolute path to the file directly:
File file = new File("/data/data//files/");
FileInputStream inStream = new FileInputStream(file);
("FileTest", readInStream(inStream));
Note: The "" in the file path above is the package where the application is located. When you write the code, you should replace it with the package that you use for your own application.
For private files, they can only be accessed by applications that create the file. If you want the file to be read and written by other applications, you can specify the Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE permissions when creating the file.
Activity also provides getCacheDir() and getFilesDir() methods:
getCacheDir() method is used to get /data/data/<package name>/cache directory
getFilesDir() method is used to get /data/data/<package name>/files directory
Many times our software needs to store or access the processed data again.Android provides a variety of ways to store data, including the following:
document
SharedPreferences
SQLite database
Content provider
network
Use files for data storage
First, let me introduce to you how to store data using files. Activity provides the openFileOutput() method that can be used to output data to a file. The specific implementation process is the same as storing data to a file in a J2SE environment.
Copy the codeThe code is as follows:
public class FileActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
...
FileOutputStream outStream = ("", Context.MODE_PRIVATE);
("CSDN".getBytes());
();
}
}
The first parameter of the openFileOutput() method is used to specify the file name and cannot contain the path separator "/". If the file does not exist, Android will automatically create it. The created file is saved in the /data/data/<package name>/files directory, such as: /data/data//files/ . By clicking on the Eclipse menu "Window" - "Show View" - "Other", expand the android folder in the dialog window, select the File Explorer view below, and then expand the /data/data/<package name>/files directory in the File Explorer view to see the file.
The second parameter of the openFileOutput() method is used to specify the operation mode. There are four modes, namely: Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
Context.MODE_PRIVATE: It is the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file. If you want to append the newly written content to the original file. Context.MODE_APPEND can be used
Context.MODE_APPEND: The mode will check whether the file exists. If it exists, it will add content to the file, otherwise a new file will be created.
Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE are used to control whether other applications have permission to read and write the file.
MODE_WORLD_READABLE: means that the current file can be read by other applications; MODE_WORLD_WRITEABLE: means that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass it in:
openFileOutput("", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
Android has its own security model. When the application (.apk) is installed, the system will assign it a userid. When the application wants to access other resources such as files, the userid needs to match it. By default, any file created by the application, sharedpreferences, and databases should be private (located in /data/data/<package name>/files) and cannot be accessed by other programs. Unless Context.MODE_WORLD_READABLE or Context.MODE_WORLD_WRITEABLE is specified at creation time, only in this way other programs can access it correctly.
Read file content
If you want to open a file stored in the /data/data/<package name>/files directory application privately, you can use Activity to provide the openFileInput() method.
FileInputStream inStream = ().openFileInput("");
("FileTest", readInStream(inStream));
Please refer to the note below this page for the readInStream() method.
Or use the absolute path to the file directly:
File file = new File("/data/data//files/");
FileInputStream inStream = new FileInputStream(file);
("FileTest", readInStream(inStream));
Note: The "" in the file path above is the package where the application is located. When you write the code, you should replace it with the package that you use for your own application.
For private files, they can only be accessed by applications that create the file. If you want the file to be read and written by other applications, you can specify the Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE permissions when creating the file.
Activity also provides getCacheDir() and getFilesDir() methods:
getCacheDir() method is used to get /data/data/<package name>/cache directory
getFilesDir() method is used to get /data/data/<package name>/files directory