One, the preservation of data----system
Btn_ok.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String user = Et_user.gettext (). toString (). Trim ();
String pwd = Et_pwd.gettext (). toString (). Trim ();
if (User.isempty () | | | Pwd.isempty ()) {
Toast.maketext (Getapplicationcontext (), "cannot Be Empty", (). Show ();
}else{
String name= "Data.txt";
String content=user+ "=" +PWD;
try {
FileOutputStream fos=openfileoutput (name, mode_private);
Fos.write (Content.getbytes ());
Fos.close ();
Toast.maketext (Getapplicationcontext (), "saved successfully", "()"). Show ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
});
the reading of the data----system
Btn_show.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String name= "Data.txt";
try {
FileInputStream fis=openfileinput (name);
BufferedReader br=new BufferedReader (New InputStreamReader (FIS));
Reading data from a document
String content = Br.readline ();
Splitting a string
string[] STRs = content.split ("=");
The displayed data
if (!content.isempty ()) {
Et_user.settext (Strs[0]);
Et_pwd.settext (Strs[1]);
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
});
second, data storage----memory card
File File=new file (Environment.getexternalstoragedirectory (), name);
try {
FileOutputStream fos=new fileoutputstream (file);
Fos.write (Content.getbytes ());
Fos.close ();
Toast.maketext (Getapplicationcontext (), "saved successfully", "()"). Show ();
} catch (Exception e) {
Toast.maketext (Getapplicationcontext (), "Save Failed", $). Show ();
E.printstacktrace ();
}
Data read----memory card
File File=new file (Environment.getexternalstoragedirectory (), name);
try {
FileOutputStream fos=new fileoutputstream (file);
Fos.write (Content.getbytes ());
Fos.close ();
Toast.maketext (Getapplicationcontext (), "saved successfully", "()"). Show ();
} catch (Exception e) {
Toast.maketext (Getapplicationcontext (), "Save Failed", $). Show ();
E.printstacktrace ();
}
Third, sharedprefrence (☆☆☆ ☆)
//Save
Btn_ok.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String user = Et_user.gettext (). toString (). Trim ();
String pwd = Et_pwd.gettext (). toString (). Trim ();
if (User.isempty () | | | Pwd.isempty ()) {
Toast.maketext (Getapplicationcontext (), "cannot Be Empty", (). Show ();
Get editor
Editor edit = Sp.edit ();
Save data
Edit.putstring ("name", user);
Edit.putstring ("pwd", PWD);
//Submit Save
Edit.commit ();
}
}
});
// read
Btn_show.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Get user Name
String user = sp.getstring ("name", null);
Get password
String pwd= sp.getstring ("pwd", null);
//Display
Et_user.settext (user);
Et_pwd.settext (PWD);
}
});
This article is from the "Android Small Notes" blog, please be sure to keep this source http://dreamwing.blog.51cto.com/9872128/1611293
Storage and reading of Android small notes