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 () 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?
  1. /**
  2. * Mainactivity
  3. *
  4. * @ Author zuolongsnil
  5. *
  6. */
  7. Public class mainactivity extends activity {
  8. Private edittext writeet;
  9. Private button writebtn;
  10. Private textview contentview;
  11. Public static final string filename = "setting. Set ";
  12. @ Override
  13. Public void oncreate (bundle savedinstancestate ){
  14. Super. oncreate (savedinstancestate );
  15. Setcontentview (R. layout. Main );
  16. Writeet = (edittext) findviewbyid (R. Id. write_et );
  17. Writebtn = (button) findviewbyid (R. Id. write_btn );
  18. Contentview = (textview) findviewbyid (R. Id. contentview );
  19. Writebtn. setonclicklistener (New operateonclicklistener ());
  20. }
  21. Class operateonclicklistener implements onclicklistener {
  22. @ Override
  23. Public void onclick (view v ){
  24. Writefiles (writeet. gettext (). tostring ());
  25. Contentview. settext (readfiles ());
  26. System. Out. println (getfilesdir ());
  27. }
  28. }
  29. // Save the file content
  30. Private void writefiles (string content ){
  31. Try {
  32. // Open the file to obtain the output stream. If the file does not exist, it is automatically created.
  33. Fileoutputstream Fos = openfileoutput (filename,
  34. Context. mode_private );
  35. FOS. Write (content. getbytes ());
  36. FOS. Close ();
  37. } Catch (exception e ){
  38. E. printstacktrace ();
  39. }
  40. }
  41. // Read the file content
  42. Private string readfiles (){
  43. String content = NULL;
  44. Try {
  45. Fileinputstream FCM = openfileinput (filename );
  46. Bytearrayoutputstream baos = new bytearrayoutputstream ();
  47. Byte [] buffer = new byte [1024];
  48. Int Len = 0;
  49. While (LEN = FS. Read (buffer ))! =-1 ){
  50. Baos. Write (buffer, 0, Len );
  51. }
  52. Content = baos. tostring ();
  53. FCM. Close ();
  54. Baos. Close ();
  55. } Catch (exception e ){
  56. E. printstacktrace ();
  57. }
  58. Return content;
  59. }
  60. }

Program:

 

Provides a tool for storing data in files:

View plaincopy to clipboardprint?
  1. /**
  2. * Tool for data storage
  3. *
  4. * @ Author zuolongsnil
  5. */
  6. Public class filesutil {
  7. /**
  8. * Save the File Content
  9. *
  10. * @ Param C
  11. * @ Param filename
  12. * File name
  13. * @ Param content
  14. * Content
  15. */
  16. Private void writefiles (context c, string filename, string content, int Mode)
  17. Throws exception {
  18. // Open the file to obtain the output stream. If the file does not exist, it is automatically created.
  19. Fileoutputstream Fos = C. openfileoutput (filename, mode );
  20. FOS. Write (content. getbytes ());
  21. FOS. Close ();
  22. }
  23. /**
  24. * Reading file content
  25. *
  26. * @ Param C
  27. * @ Param filename
  28. * File name
  29. * @ Return returns the File Content
  30. */
  31. Private string readfiles (context c, string filename) throws exception {
  32. Bytearrayoutputstream baos = new bytearrayoutputstream ();
  33. Fileinputstream FCM = C. openfileinput (filename );
  34. Byte [] buffer = new byte [1024];
  35. Int Len = 0;
  36. While (LEN = FS. Read (buffer ))! =-1 ){
  37. Baos. Write (buffer, 0, Len );
  38. }
  39. String content = baos. tostring ();
  40. FCM. Close ();
  41. Baos. Close ();
  42. Return content;
  43. }
  44. }

Related Article

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.