Android Note 20170117

Source: Internet
Author: User

Wirelessly file storage

The Openfileoutput method in the context class that can be used to store the data in the specified file. This method accepts two parameters, the first parameter is the file name, and is the name that is used when the document is created. The file name specified here may not contain a path. Because all files are stored by default in the/data/data/<package name>/files/directory.

The second parameter is the mode of operation of the file, and there are two main modes to choose from:

Mode_private: The default mode of operation, which means that when the same file name is specified, the content that is written overwrites the contents of the original file.

Mode_append: Indicates that if the file already exists, append the contents to the file and create a new file if it does not exist.

The Openfileoutput method returns a FileOutputStream object.

The context class also provides a Openfileinput method for reading data from a file. Receives only one parameter, which is the name of the file to be read, and then the system automatically loads the file into the/data/data/<package name>/files/directory and returns a FileInputStream object.

Here is a simple example, open the app, enter the data in EditText, close the app in the Ondestory method to save data, open the app again, read the data displayed in EditText

 Public classMainactivityextendsActivity {PrivateEditText Edit; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Edit=(EditText) Findviewbyid (R.id.edit); String Inputtext=load (); if(!Textutils.isempty (Inputtext))            {Edit.settext (Inputtext);            Edit.setselection (Inputtext.length ()); Toast.maketext ( This, "Restoring succeeded", Toast.length_short). Show (); }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); String Inputtext=Edit.gettext (). toString ();    Save (Inputtext); }         Public voidSave (String inputtext) {FileOutputStream out=NULL; BufferedWriter writer=NULL; Try{ out= Openfileoutput ("Data", context.mode_private); Writer=NewBufferedWriter (NewOutputStreamWriter (out));        Writer.write (Inputtext); } Catch(IOException e) {e.printstacktrace (); }finally{            Try {                if(Writer! =NULL) {writer.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }    }         PublicString Load () {FileInputStream in=NULL; BufferedReader Reader=NULL; StringBuilder content=NewStringBuilder (); Try{ in= Openfileinput ("Data"); Reader=NewBufferedReader (NewInputStreamReader (in)); String Line= "";  while(line = Reader.readline ())! =NULL) {content.append (line); }        } Catch(IOException e) {e.printstacktrace (); }finally{            if(Reader! =NULL){                Try{reader.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }        returncontent.tostring (); }}

The Textutils.isempty () method allows two null values to be judged at once. This method returns True when the passed-in string equals null or is equal to an empty string, so that we do not need to judge the two null values individually and then use the logical operators to join them.

Android Note 20170117

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.