"Android" Data storage-java IO stream file storage

Source: Internet
Author: User

1. Data persistence: The instantaneous data in memory is saved on the storage device. Instantaneous data: Device shutdown data loss. Persistence technology provides a mechanism for converting data between transient and persistent states.

Three simple storage methods in 2.Android: File storage, sharedpreference storage, and database storage.

1. File storage: No processing of data, the data is stored intact in the file, suitable for storing some simple text data and binary data.

A. Storing data in a file

The context class provides a openfileoutput () method that can be used to store data in a file. This method receives two parameters, the first parameter is the file name (the file name cannot contain the path, the default path is/data/data/<packagename>/files/), the second parameter is the mode of operation of the files, mainly Mode_ PRIVATE and Mode_append Two modes, the former is the default mode, when the same file name is specified, the content will overwrite the contents of the original file, the latter indicates that the file name is appended to the original file content, the files do not exist when the new file is created. The Openfileoutput method returns a FileOutputStream object.

 Public voidSave () {String data= "Data to save"; FileOutputStream out=NULL; BufferedWriter writer=NULL;Try{ out= Openfileoutput ("Data", context.mode_private); writer=NewBufferedWriter (NewOutputStreamWriter (out)); Writer.write (data);} Catch(IOException e) {e.printstacktrace ();}finally {Try {if(Writer! =NULL) {writer.close ();}} Catch(IOException e) {e.printstacktrace ();}}}
/*
Steps
* * through the Openfileoutput () method to a FileOutputStream object
* *. Constructs a OutputStreamWriter object from the FileOutputStream object
BufferedWriter object constructed from OutputStreamWriter object
Write text content to a file via BufferedWriter
*/

B. Reading data from a file

Similar to storing data in text, a Openfileinput () method is provided in the context class for reading data from text. This method only takes one parameter to read the file name of the file, the system finds the file in the default directory and returns a Filleinputstream object, which can be read by the Java IO Stream after the object is obtained.

 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 ();}

/*
Get to FileInputStream object first through Openfileinput
Get to InputStreamReader object via FileInputStream
Get BufferedReader objects by InputStreamReader
Read all the text contents of the file in the StringBuilder by BufferedReader a row of rows.
A. Returns the contents of the read to
*/

   

"Android" Data storage-java IO stream file storage

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.