Io of Android data storage

Source: Internet
Author: User
Tags gettext

Data storage is unavoidable in Android development, and today we talk about how to use IO streams to store data.

Here we actually operate the IO stream by simulating a small demo of the QQ landing interface.

Function Description: Click the button can save user input user name and password, when the click Remember Password, also can be opened in the application the second time, echo the user name and password

1. The code of the layout file here is not posted, see

2.mainactivity.java

 PackageCom.example.viewswitchtest;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;ImportJava.util.ArrayList;ImportJava.util.List;ImportAndroid.os.Bundle;ImportAndroid.os.Environment;Importandroid.app.Activity;Importandroid.graphics.drawable.Drawable;ImportAndroid.telephony.SmsManager;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {    PrivateEditText Editaccount;PrivateEditText editpwd;PrivateButton Btnlogin;PrivateCheckBox checkbox;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Account EditEditaccount = (EditText) Findviewbyid (R.id.edit_account);//Password editEditpwd = (EditText) Findviewbyid (R.ID.EDIT_PWD);//Login buttonBtnlogin = (Button) Findviewbyid (R.id.btn_login);//Remember account number and password checkboxcheckbox = (checkbox) Findviewbyid (R.id.checkbox);//Echo account and passwordReadaccountandpwd (); Btnlogin.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View arg0) {if(Checkbox.ischecked ())                    {String account = Editaccount.gettext (). toString (); String pwd = Editpwd.gettext (). toString ();//Android to prevent developer handwriting path errors, the gate provides two APIs                    //Getfilesdir () and Path: data/data/Project package name/files                    //Getcachedir () and Path: data/data/Project package name/cacheFile File =NewFile (Getfilesdir (),"Info.txt");Try{FileOutputStream FOS =NewFileOutputStream (file); Fos.write (account +"-"+ pwd). GetBytes ());//Save format as "Account-password" for easy segmentationFos.close (); }Catch(Exception e)                    {E.printstacktrace (); }} toast.maketext (mainactivity. This,"Login Success", Toast.length_long). Show ();    }        }); }/** * echo user name and password */    Private void readaccountandpwd() {File File =NewFile (Getfilesdir (),"Info.txt");if(File.exists ()) {Try{FileInputStream FIS =NewFileInputStream (file); BufferedReader reader =NewBufferedReader (NewInputStreamReader (FIS));                String text = Reader.readline (); string[] Infos = Text.split ("-");//Echo InformationEditaccount.settext (infos[0]); Editpwd.settext (infos[1]);                Fis.close ();            Reader.close (); }Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); }        }    }}

3. Enter the account number and password, click the login button, we can see in the Ddms file explore in the saved TXT format files:

4. Export the file, open the verification through a text editor, the account password is correct:

You can see that the account and password are saved correctly and in the correct format.

5. Close the app and open it again

You can see that the data can be correctly echoed into the input box,:-D!!!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Io of Android data storage

Related Article

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.