Data read for Android internal storage files

Source: Internet
Author: User

1:android development, it is inseparable from the operation of the file, because the file can store a large amount of data, mastering the relevant technology can bring a lot of convenience for development, is our application in the mobile phone or simulator on the internal storage location.

2: Through a simple program to achieve the Android application of internal files read operations, the page is as follows:

3: The sample code is as follows:

 PackageCativity.cyq.readwriteintemaldata;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException; Public classMainactivityextendsactionbaractivity {Private FinalString file_name = "Text.txt"; PrivateButton writrbtn; PrivateButton readbtn; PrivateEditText Writeedit; PrivateTextView Showtext; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Final file Filename=new file (file_name);WRITRBTN =(Button) Findviewbyid (R.ID.WRITE_BTN); READBTN=(Button) Findviewbyid (R.ID.READ_BTN); Writeedit=(EditText) Findviewbyid (R.id.write_edit); Showtext=(TextView) Findviewbyid (R.id.show_text); Writrbtn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Try {                    /*The Openfileoutput () method is called, and if the file exists, the file is created only if it does not exist. Mode_append means appending data at the end of the text, without overwriting the original data * Context.mode_private: The default mode of operation, which means that the file is private and can only be accessed by the app itself, in which the content written overwrites the original file                    Context.mode_append: Mode checks whether the file exists, appends content to the file, or creates a new file. Mode_world_readable: Indicates that the current file can be read by another application; Mode_world_writeable: Indicates that the current file can be written by another application. */FileOutputStream Fos=openfileoutput (file_name, mode_append); OutputStreamWriter OSW=NewOutputStreamWriter (FOS, "UTF-8");                    Osw.write (Writeedit.gettext (). toString ()); //osw.write ("Hello Wrold!");Osw.flush ();                    Fos.flush ();                    Osw.close ();                    Fos.close (); Toast.maketext (mainactivity. This, "Write succeeded", Toast.length_short). Show (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace ();        }            }        }); Readbtn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Try{fileinputstream fps=Openfileinput (file_name); InputStreamReader ISR=NewInputStreamReader (fps, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); StringBuilder Sbuilder=NewStringBuilder (); String Line= "";  while(line = Br.readline ())! =NULL) {sbuilder.append (line);                    } showtext.settext (Sbuilder.tostring ()); Toast.maketext (mainactivity. This, "Read succeeded", Toast.length_short). Show (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace ();    }            }        }); }}

Data read for Android internal storage files

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.