How Android saves and reads data in file Form _android

Source: Internet
Author: User

This example describes how Android saves and reads data in file format. Share to everyone for your reference, specific as follows:

To secure the data directly in the form of a file in the device, the standard Java file input stream (FileInputStream) is obtained through the Context.openfileinput () method, through Context.openfileoutput () method to obtain standard Java file output stream (FileOutputStream)

Write data to file files

Findviewbyid (R.id.file). Setonclicklistener (New Button.onclicklistener () {
  @Override public
  void OnClick ( View v {
    try {
    //writable way to create or open Huangbiao.txt file
    //the path to the file is/data/data/package name/files/huangbiao.txt
      FileOutputStream fos = openfileoutput ("Huangbiao.txt", context.mode_append);
    Writes a string to a file
      fos.write ("Huangbiao". GetBytes ());
    Close Data Flow
      fos.close ();
    } catch (FileNotFoundException e) {
      //TODO auto-generated catch
      block E.printstacktrace ();
    } catch (IOException e) {
      //TODO auto-generated catch block
      e.printstacktrace ();
    }}}
);

Ways to read data

Findviewbyid (R.id.read_file). Setonclicklistener (New Button.onclicklistener () {
  @Override public
  Void OnClick (View v) {
    FileInputStream fis;
    try {
    //Open the file and get the InputStream object
      fis = openfileinput ("Huangbiao.txt");
    Available () returns the estimated space length
      byte[] buffer = new byte[fis.available ()];
    The content of the data stream is written into buffer
      fis.read (buffer);
      String AAA = new string (buffer);
      System.out.println (AAA);
      Fis.close ();
    } catch (FileNotFoundException e) {
      //TODO auto-generated catch block
      e.printstacktrace ();
    } catch ( IOException e) {
      //TODO auto-generated catch block
      e.printstacktrace ();
    }}}
);

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.