1. The file storage data uses Io operations in Java to store and read files, but Android encapsulates the methods for retrieving input and output streams in the context class.
The created storage file is saved in the/data/<package name>/Files folder.
2. operation.
Save the file content: Get the output stream through context. openfileoutput. The parameters are file names and storage modes respectively.
Read File Content: Get the input stream through context. openfileinput. The parameter is the file name.
Delete file: context. deletefile: Delete the specified file. The parameter is the name of the file to be deleted.
Get File Name List: Use context. filelist to get all file name arrays in the files directory.
* Method for obtaining the file path:
Absolute path:/data/<package name>/files/filename
Context: context. getfilesdir () to get "/data/<package name>/Files"
3. Four file storage modes.
Context. mode_private is the default operation mode, which indicates that the file is private data and can only be accessed by the application itself. The content written in this mode will overwrite the content of the original file.
Context. mode_append: Check whether the file exists. If the file exists, append the content to the file. Otherwise, create a new file.
Mode_world_readable indicates that the current file can be read by other applications.
Mode_world_writeable indicates that the current file can be written by other applications.
You can use "+" to select multiple modes, such as openfileoutput (filename, context. mode_private + mode_world_readable );
The following uses a program to demonstrate the use of file storage. Download the complete code: android_files.rar
View plaincopy to clipboardprint?
- /**
- * Mainactivity
- *
- * @ Author zuolongsnil
- *
- */
- Public class mainactivity extends activity {
- Private edittext writeet;
- Private button writebtn;
- Private textview contentview;
- Public static final string filename = "setting. Set ";
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Writeet = (edittext) findviewbyid (R. Id. write_et );
- Writebtn = (button) findviewbyid (R. Id. write_btn );
- Contentview = (textview) findviewbyid (R. Id. contentview );
- Writebtn. setonclicklistener (New operateonclicklistener ());
- }
- Class operateonclicklistener implements onclicklistener {
- @ Override
- Public void onclick (view v ){
- Writefiles (writeet. gettext (). tostring ());
- Contentview. settext (readfiles ());
- System. Out. println (getfilesdir ());
- }
- }
- // Save the file content
- Private void writefiles (string content ){
- Try {
- // Open the file to obtain the output stream. If the file does not exist, it is automatically created.
- Fileoutputstream Fos = openfileoutput (filename,
- Context. mode_private );
- FOS. Write (content. getbytes ());
- FOS. Close ();
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- // Read the file content
- Private string readfiles (){
- String content = NULL;
- Try {
- Fileinputstream FCM = openfileinput (filename );
- Bytearrayoutputstream baos = new bytearrayoutputstream ();
- Byte [] buffer = new byte [1024];
- Int Len = 0;
- While (LEN = FS. Read (buffer ))! =-1 ){
- Baos. Write (buffer, 0, Len );
- }
- Content = baos. tostring ();
- FCM. Close ();
- Baos. Close ();
- } Catch (exception e ){
- E. printstacktrace ();
- }
- Return content;
- }
- }
Program:
Provides a tool for storing data in files:
View plaincopy to clipboardprint?
- /**
- * Tool for data storage
- *
- * @ Author zuolongsnil
- */
- Public class filesutil {
- /**
- * Save the File Content
- *
- * @ Param C
- * @ Param filename
- * File name
- * @ Param content
- * Content
- */
- Private void writefiles (context c, string filename, string content, int Mode)
- Throws exception {
- // Open the file to obtain the output stream. If the file does not exist, it is automatically created.
- Fileoutputstream Fos = C. openfileoutput (filename, mode );
- FOS. Write (content. getbytes ());
- FOS. Close ();
- }
- /**
- * Reading file content
- *
- * @ Param C
- * @ Param filename
- * File name
- * @ Return returns the File Content
- */
- Private string readfiles (context c, string filename) throws exception {
- Bytearrayoutputstream baos = new bytearrayoutputstream ();
- Fileinputstream FCM = C. openfileinput (filename );
- Byte [] buffer = new byte [1024];
- Int Len = 0;
- While (LEN = FS. Read (buffer ))! =-1 ){
- Baos. Write (buffer, 0, Len );
- }
- String content = baos. tostring ();
- FCM. Close ();
- Baos. Close ();
- Return content;
- }
- }