Previously, I briefly introduced two data storage methods, for example, shared preferences, in [Android evolution 14th], I wrote an example of the address book for storing data in the SQLite database. Next I will introduce the format of storing data in file. I will talk about two points in file format, files cannot be shared among different programs. Another feature is to use the openfileoutput method to open a file (create this file if the file does not exist). openfileoutput saves things to the file. I wrote an example: click the button and write a count of the int type in the file. divide it by 2 to get the remainder. If the remainder is 0, the status is off, if the remainder is 1, the status is on. The Count value is always ++. This is a simple example. Reprinted please indicate the source:Http://blog.csdn.net/wdaming1986/article/details/6849703
See:Click the button and click the button again.
This is the content in the dmfile. cfg file: mbutton always ++. If it is an odd number, mchange = false. If it is an even number, it is true.
Code: In this filestorage project:
In the com.cn. Daming package: Content in the filestoragemainactivity. Java class:
Package com.cn. daming; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. util. properties; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. keyevent; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. W Idget. textview; import android. widget. toast; public class filestoragemainactivity extends activity {private textview mtextview; private button cbutton; private Boolean mchanged = false; Public int mcount = 0; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtextview = (textview) findviewbyid (R. id. show_textview); cbutton = (B Utton) findviewbyid (R. id. button); // read the mchanged is true or false readloadfile (); refretextview (); initbutton ();} private void initbutton () {cbutton. setonclicklistener (New onclicklistener () {public void onclick (view arg0) {readloadfile (); If (mcount % 2 = 0) {mchanged = false; refretextview ();} if (mcount % 2 = 1) {mchanged = true; refretextview () ;}mcount ++; savefilestorage () ;}} public void refretextview () {I F (mchanged) {mtextview. settext ("the current status is: open! ");} Else {mtextview. settext (" Current status: off! ") ;}@ Overridepublic Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) {// Save the filestorage before the programe backsavefilestorage (); If (mchanged) {toast. maketext (this, "the exit state of the program is open! ", Toast. length_long). Show ();} else {toast. maketext (this," the exit status of the program is off! ", Toast. length_long ). show ();} This. finish (); Return true;} return Super. onkeydown (keycode, event);} private void readloadfile () {// make the properties Properties = new properties (); try {fileinputstream istream = This. openfileinput ("dmfile. cfg "); properties. load (istream);} catch (filenotfoundexception e) {return;} catch (ioexception e) {return;} mchanged = Boolean. valueof (properties. getproperty ("mchanging "). tostring (); mcount = integer. valueof (properties. getproperty ("mbutton "). tostring ();} private Boolean savefilestorage () {properties Properties = new properties (); properties. put ("mchanging", String. valueof (mchanged); properties. put ("mbutton", String. valueof (mcount); try {fileoutputstream ostream = This. openfileoutput ("dmfile. cfg ", context. mode_world_writeable); properties. store (ostream, "");} catch (filenotfoundexception e) {return false;} catch (ioexception e) {return false;} return true ;}}
Content in the main. xml file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello" Android: gravity = "center_vertical | regular" Android: layout_marginbottom = "15dip" Android: layout_margintop = "15dip"/> <button Android: Id = "@ + ID/button" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "test and change file content" Android: layout_marginbottom = "10dip"/> <textview Android: Id = "@ + ID/show_textview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center"/> </linearlayout>