File Storage Data in Android

Source: Internet
Author: User

 

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 () can 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 complete code:

Click the link to download android_files
The size is 44.29 kb.
Downloads: 0
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
/*** 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_e T); 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 priva Te void writefiles (string content) {try {// open the file to obtain the output stream. If the file does not exist, the 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 ISI = openfileinput (filename); bytearrayoutputstream baos = new bytearrayoutputstream (); Byte [] buffer = new byte [1024]; int Len = 0; while (LEN = Fi. 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:

123456789101112131415161718192021222324252627282930313233343536373839404142434445
/*** File storage data method tool class ** @ author zuolongsnil */public class filesutil {/*** save file content ** @ Param C * @ Param filename * file name * @ Param 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, the fileoutputstream Fos = C is automatically created. openfileoutput (filename, mode); FOS. write (content. getbytes (); FOS. close ();}/*** Read File Content ** @ Param C * @ P Aram filename * file name * @ return returned file content */private string readfiles (context c, string filename) throws exception {bytearrayoutputstream baos = new bytearrayoutputstream (); fileinputstream FD = C. openfileinput (filename); byte [] buffer = new byte [1024]; int Len = 0; while (LEN = Fi. read (buffer ))! =-1) {baos. write (buffer, 0, Len);} string content = baos. tostring (); FCM. close (); baos. close (); Return content ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.