Android Learning notes (14)

Source: Internet
Author: User
<span id="Label3"></p><p><p>Data storage in Android</p></p><p><p>Data persistence means that the instantaneous data in memory is saved to the storage device. There are three main ways in which Android systems are designed to simply implement</p></p><p><p>Data persistence, which is file storage, sharedpreferences storage, and database Storage.</p></p><p><p></p></p><p><p>1. File storage is a basic method of storage in android, it does not do any formatting of the stored content, all the data is intact</p></p><p><p>Saved to a File.</p></p><p><p>1.1</p></p><p><p>A Openfileoutput () method is provided in the context class that can be used to store data to the specified File.</p></p><p><p>This method receives two Parameters:</p></p><p><p>1. The first parameter is the file name, (the file name is not included in the path, all files are saved by default in The/data/data/<package name>/files/directory).</p></p><p><p>2. The second parameter is the operating mode of the file (there are two main modes of Operation: 1. Mode_private and mode_append, of which</p></p><p><p>Mode_private is the default mode of operation, indicating that when the same file name is specified, the content written will overwrite the contents of the original file;</p></p><p><p>Mode_append indicates that if the file already exists, append content to the file and create a new file if it does not exist. )</p></p><p><p></p></p><p><p>The Openfileoutput () method returns a FileOutputStream object that can be used to write the data in a file using the Java Stream.</p></p><p><p>The following code shows how to write a piece of text in a file:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Save (string Inputtext) {string data</span></span>= "Data to Save"<span style="color: #000000;"><span style="color: #000000;">; FileOutputStream</span> out</span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; BufferedWriter writer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{</span> out</span>= Openfileoutput ("data"<span style="color: #000000;"><span style="color: #000000;">, context.mode_private); Writer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>BufferedWriter (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">OutputStreamWriter (out)); Writer.write (inputtext); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(writer! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {writer.close (); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } } }</span></span></pre><p><p>In the code above, a FileOutputStream object can be obtained through the Openfileoutput () method, which is then used to build a</p></p><p><p>OutputStreamWriter object, and then uses OutputStreamWriter to build a BufferedWriter object so that it can be</p></p><p><p>BufferedWriter to write text content to a File.</p></p><p><p></p></p><p><p>1.2</p></p><p><p>A Openfileinput () method is also provided in the context class for reading data from a File.</p></p><p><p>This method takes only one parameter, which is the name of the file to be read, and then the system will automatically go to The/data/data/<package name>/files/directory</p></p><p><p>To load the file and return a FileInputStream object that can be read by the Java stream after the object is Obtained.</p></p><p><p>The following is a code example that shows how to read text data from a file:</p></p><pre>FileInputStream in =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; BufferedReader Reader</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; StringBuilder content</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{</span> in</span>= Openfileinput ("data"<span style="color: #000000;"><span style="color: #000000;">); Reader</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>BufferedReader (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">InputStreamReader (in)); String</span> line</span>= ""<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(line = Reader.readline ())! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {content.append (line); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(reader! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{reader.close (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">content.tostring (); }</span></span></pre><p><p>In the above code, first obtains a FileInputStream object through the Openfileinput () method, then constructs a InputStreamReader object with it,</p></p><p><p>Then use InputStreamReader to build a bufferedreader object, so that you can use the BufferedReader to read a line, the file</p></p><p><p>The contents of the StringBuilder are all read out and present in a single object, and then the contents of the read are Returned.</p></p><p><p></p></p><p><p></p></p><p><p>2.SharedPreferences is the way to store data using Key-value Pairs.</p></p><p><p></p></p><p><p>2.1</p></p><p><p>To store data with sharedpreferences, you first need to get to the sharedpreferences Object. There are three main methods available in Android for</p></p><p><p>Get the Sharedpreferences Object.</p></p><p><p>1). The Getsharedpreferences () method in the context class</p></p><p><p>The method receives two parameters, the first parameter specifies the name of the Sharedpreferences file, and if the specified file name does not exist, it creates a</p></p><p><p>Sharedpreferences files are present in The/data/data/<package name>/shared_prefs/directory;</p></p><p><p>The second parameter specifies the mode of operation, and there are two main modes to choose from, mode_private and Mode_multi_process.</p></p><p><p>Where mode_private is the default mode of operation, which means that only the current application can read and write to this sharedpreferences file.</p></p><p><p>Mode_multi_process is typically used to read and write to the same sharedpreferences file in a process.</p></p><p><p>2). The Getpreferences () method in the Activity class</p></p><p><p>3). getdefaultsharedpreferences () method in the Preferencemanager class</p></p><p><p></p></p><p><p>After the Sharedpreferences object is obtained, the data can be stored in the file, which can be divided into three implementations:</p></p><p><p>1). Call the edit () method of the Sharedpreferences object to get a sharedpreferences.editor object.</p></p><p><p>2). add data to the Sharedpreferences.editor Object. For example, Add a string and use the putstring () method.</p></p><p><p>3). Call the Commit () method to submit the added Data.</p></p><p><p>The specific implementation is shown in the following code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/save_data"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/save_data"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Set a button to store the data in the File.</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); SaveData</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.save_data); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">setting up listeners for SaveData</span></span>Savedata.setonclicklistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">View.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Call the edit () method of the Sharedpreferences object to get a sharedpreferences.editor object. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">getsharedpreferences () method in the context class to get the Sharedpreferences object, which receives two parameters</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. The first parameter specifies the name of the Sharedpreferences file, and if the specified file does not exist, one is Created. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. The second parameter is used to specify the operating mode, there are two main modes of Operation (mode_private and Mode_multi_process)</span></span>Sharedpreferences.editor Editor = getsharedpreferences ("data"<span style="color: #000000;"><span style="color: #000000;">, mode_private). Edit (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adds data to the Sharedpreferences.editor Object. For example, Add a string and use the Putstring () method</span></span>Editor.putstring ("name", "Tom"<span style="color: #000000;"><span style="color: #000000;">); Editor.putint (</span></span>"age", 22<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Call the Commit () method to submit the added data</span></span><span style="color: #000000;"><span style="color: #000000;">Editor.commit (); } });</span></span></pre><p><p>The above code, click the button through the Getsharedpreferences () method to get the Sharedpreferences file name of data, and got the</p></p><p><p>The Sharedpreferences.editor Object. Then add the data and finally call the commit () method to Commit.</p></p><p><p></p></p><p><p>2.2</p></p><p><p>Reading data from the Sharedpreferences</p></p><p><p>The Sharedpreferences object provides a series of get methods for reading stored data, each of which corresponds to the</p></p><p><p>A put method in Sharepreferences.editor.</p></p><p><p>The specific implementation is shown in the following code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/save_data"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/save_data"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/restore_data"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@string/restore_data"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Modify the code in the mainactivity as shown in:</p></p><pre>Restoredata =<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.restore_data); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the listener on the Restoredata button</span></span>Restoredata.setonclicklistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">View.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get Sharedpreferences Object by Getsharedpreferences () method</span></span>Sharedpreferences Sharedpre = getsharedpreferences ("data")<span style="color: #000000;"><span style="color: #000000;">, mode_private); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the data through the Get method</span></span>String name = sharedpre.getstring ("name", "" "<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Age = Sharedpre.getint ("age", 0<span style="color: #000000;"><span style="color: #000000;">); LOG.D (</span></span>"mainactivity", "name is" +<span style="color: #000000;"><span style="color: #000000;">name); LOG.D (</span></span>"mainactivity", "age is" +<span style="color: #000000;">age <span style="color: #000000;">); } });</span></span></pre><p><p>In the above code, the Sharedpreferences object is first obtained through the getsharedpreferences () method, and then the Get method is called to get the Data.</p></p><p><p>If no corresponding value is found, the default value passed in the method is used Instead.</p></p><p><p></p></p><p><p>Android Learning notes (14)</p></p></span>

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.