Use file files for data storage in Android

Source: Internet
Author: User

 use file files for data storage in Android

Last learned to use sharedperences for data storage, then learn to use file for storage we sometimes can save the data directly in the form of a file in the device,

Example: text files, picture files, etc.

Using file for storage operations mainly uses the following

①:p ublic abstract FileInputStream openfileinput (String name)

This is mostly open file, return FileInputStream

②:p ublic abstract fileoutputstream openfileoutput (String name, int mode)

This is mostly written to the file, if the file does not exist, directly to create, return FileOutputStream

Mode (mainly with the following four types of modes)

Mode_append//Append at tail
Mode_private//Private
Mode_world_readable//readable
Mode_world_writeable//Writable

③:fileinputstream (get file input stream) and FileOutputStream (get file output stream) These two classes are common in Javaio operations

After the operation is saved successfully, the file is saved in the current files/of the program's package name (other paths that can be changed for storage)

The source code is as follows:

[Java]View Plaincopy
  1. Package com.jiangqq.file;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.FileOutputStream;
  4. Import android.app.Activity;
  5. Import Android.os.Bundle;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.widget.Button;
  9. Import Android.widget.EditText;
  10. Import Android.widget.Toast;
  11. Public class Fileacitivy extends Activity
  12. {
  13. private Button bt1, BT2;
  14. private EditText et1, Et2;
  15. private static final String FILENAME = "Temp_file.txt";
  16. @Override
  17. public void OnCreate (Bundle savedinstancestate) {
  18. super.oncreate (savedinstancestate);
  19. Setcontentview (R.layout.main);
  20. BT1 = (Button) This.findviewbyid (R.ID.BT1);
  21. BT2 = (Button) This.findviewbyid (R.ID.BT2);
  22. Et1 = (EditText) This.findviewbyid (R.ID.ET1);
  23. Et2 = (EditText) This.findviewbyid (R.id.et2);
  24. Bt1.setonclicklistener (new Onclicklistener () {
  25. @Override
  26. public void OnClick (View v) {
  27. Write (Et1.gettext (). toString ());
  28. Toast.maketext (Fileacitivy. This, "file write succeeded", Toast.length_long)
  29. . Show ();
  30. }
  31. });
  32. Bt2.setonclicklistener (new Onclicklistener () {
  33. @Override
  34. public void OnClick (View v) {
  35. Et2.settext (read ());
  36. Toast.maketext (Fileacitivy. This, "file read success", Toast.length_long)
  37. . Show ();
  38. }
  39. });
  40. }
  41. //Read File method
  42. private String Read () {
  43. try {
  44. FileInputStream InputStream = Openfileinput (FILENAME);
  45. byte[] B = new byte[inputstream.available ()];
  46. Inputstream.read (b);
  47. return new String (b);
  48. } catch (Exception e) {
  49. }
  50. return null;
  51. }
  52. //write file
  53. private void Write (String content) {
  54. try {
  55. FileOutputStream fos = openfileoutput (FILENAME, mode_append);
  56. Fos.write (Content.getbytes ());
  57. Fos.close ();
  58. } catch (Exception e) {
  59. }
  60. }
  61. }

Source code Download Link:

http://115.com/file/c2mu1pfd#
Activity_18_file.rar

Original: http://blog.csdn.net/developer_jiangqq/article/details/7039852

Use file files for data storage in Android

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.