Background
We have a common log-in time checkbox to show whether to save account password form to vividly explain the story.
Finally, the TXT document is saved with the Java IO operation.
This is only rough, we do not like to spray.
Save File
Public Static voidSavefile2card (Context context,string username,string password) {File file=NULL; FileOutputStream Fos=NULL; Try { //file = new file ("/data/data/com.yuyidong.savefile/savefile.txt");File =NewFile (Context.getfilesdir (), "Info.txt"); FOS=Newfileoutputstream (file); Fos.write ((username+"!!!!" +password). getBytes ()); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); Try{fos.close (); } Catch(IOException E1) {//TODO Auto-generated catch blockE1.printstacktrace (); } } }
Read file
Public StaticMap<string,string>Getsavefile (Context context) {File file=NewFile (Context.getfilesdir (), "Info.txt"); Try{FileInputStream fis=Newfileinputstream (file); BufferedReader BR=NewBufferedReader (NewInputStreamReader (FIS)); String Str=Br.readline (); String[] Infos= Str.split ("!!!!"); Map<String,String> map =NewHashmap<string, string>(); Map.put ("Username", infos[0]); Map.put ("Password", infos[1]); Br.close (); returnmap; } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); return NULL; } finally { } }
Main program
Public classMainactivityextendsActivity {Privatebutton button; PrivateCheckBox Check; PrivateEditText Usernametext; PrivateEditText Passwordtext; PrivateString username; PrivateString password; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Check=(CheckBox) Findviewbyid (R.id.check);
= (EditText) Findviewbyid (r.id.username); = (EditText) Findviewbyid (R.id.password);
Button.setonclicklistener (new Buttonlistener ());
Map<string, string> map = Read.getsavefile ( This); Usernametext.settext (Map.get ("Username")); Passwordtext.settext (Map.get ("Password")); } classButtonlistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {//TODO Auto-generated method stubsUsername =Usernametext.gettext (). toString (); Password=Passwordtext.gettext (). toString (); SYSTEM.OUT.PRINTLN (username+"~!! ~"+password); if(check.ischecked ()) {Save.savefile2card (mainactivity). This, username, password); } } }
I'm the dividing line of the king of the Land Tiger.
Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01
Savefile.zip
Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3708964.html