Storage and reading of Android small notes

Source: Internet
Author: User

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

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.