Internal storage file (read)

Source: Internet
Author: User

Code that is not optimized:

Key features of the implementation:

1. When you enter the user password and click Log in, first determine whether the check box is checked, if checked, the user name, password through the file write method written to the internal storage info.txt.

2. When the app starts, execute the function readaccount () function, which is displayed in the User name and password box by means of a file read method (one row only) and the line string is split.

The code is as follows:

 PackageCom.swust.intern;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                                Setcontentview (R.layout.activity_main); //after the app starts, read the archived user name and passwordReadaccount (); }     Public voidReadaccount () {File file=NewFile ("Data/data/com.swust.intern/info.txt"); Try{FileInputStream fis=Newfileinputstream (file); //turn bytes into a stream of charactersBufferedReader br =NewBufferedReader (NewInputStreamReader (FIS)); //user name and password to read TXT file//read a rowString Text =Br.readline (); //use "# #" to identify the cut and get an array of stringsString[] s = Text.split ("# #"); EditText Et_name=(EditText) Findviewbyid (r.id.et_name); EditText et_pwd=(EditText) Findviewbyid (R.ID.ET_PWD); Et_name.settext (s[0]); Et_pwd.settext (s[1]); }Catch(Exception e) {e.printstacktrace (); }    }     Public voidLogin (View v) {EditText et_name=(EditText) Findviewbyid (r.id.et_name); EditText et_pwd=(EditText) Findviewbyid (R.ID.ET_PWD); CheckBox CB=(CheckBox) Findviewbyid (R.ID.CB); String name=Et_name.gettext (). toString (); String pwd=Et_pwd.gettext (). toString (); //check box is checked        if(cb.ischecked ()) {//"Data/data/com.swust.intern" is the internal storage space pathFile File =NewFile ("Data/data/com.swust.intern/info.txt");            FileOutputStream Fos; Try{fos=Newfileoutputstream (file); //Plus "# #" is for good segmentation when readingFos.write ((name + "# #" +pwd). GetBytes ());            Fos.close (); }Catch(Exception e) {e.printstacktrace (); }        }        //System.out.println ("Landing success"); //Create a Toast dialog box        /*The first parameter on the context, and activity is originally the subclass of the context, so directly fill this*/        /*The third parameter is the continuous display time, with only Length_short (2S) and Length_long (5s) Options*/        /*Toast t= Toast.maketext (This, "landing success", Toast.length_short); Show Toast dialog box T.show ();*/Toast.maketext ( This, "Successful Landing", Toast.length_short). Show (); }}

Of course, there are drawbacks:

First, the code is not optimized, and the FindByID method is implemented several times and should be resolved using global variables.

Second, the logical loophole, 1. When the application first starts, it is unable to read the account password

2. When the user does not choose to remember the account name and password at a later time, then the TXT file should be emptied instead of the previous old account password

Optimization solution (code optimization and first boot judgment file presence):

 PackageCom.swust.intern;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {private EditText Et_name; Private EditText et_pwd; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //is already a global variable, get id et_name = (EditText) Findviewbyid (r.id.et_name) when app starts;                Et_pwd = (EditText) Findviewbyid (R.ID.ET_PWD); //after the app starts, read the archived user name and passwordReadaccount (); }     Public voidReadaccount () {File file=NewFile ("Data/data/com.swust.intern/info.txt");
      

To interpret whether a file exists
if (file.exists ()) {
try{
FileInputStream fis =new fileinputstream (file);
Turn bytes into a stream of characters
BufferedReader BR =new BufferedReader (new InputStreamReader (FIS));
User name and password to read TXT file
Read a row
String text =br.readline ();
Use "# #" to identify the cut and get an array of strings
String[] s = Text.split ("# #");


Et_name.settext (S[0]);
Et_pwd.settext (S[1]);

}catch (Exception e) {
E.printstacktrace ();
}
}

} Public voidLogin (View v) {CheckBox CB=(CheckBox) Findviewbyid (R.ID.CB); String name =et_name.gettext (). toString ();        Directly using the String pwd =et_pwd.gettext (). toString (); //check box is checked        if(cb.ischecked ()) {//"Data/data/com.swust.intern" is the internal storage space pathFile File =NewFile ("Data/data/com.swust.intern/info.txt");            FileOutputStream Fos; Try{fos=Newfileoutputstream (file); //Plus "# #" is for good segmentation when readingFos.write ((name + "# #" +pwd). GetBytes ());            Fos.close (); }Catch(Exception e) {e.printstacktrace (); }        }        //System.out.println ("Landing success"); //Create a Toast dialog box        /*The first parameter on the context, and activity is originally the subclass of the context, so directly fill this*/        /*The third parameter is the continuous display time, with only Length_short (2S) and Length_long (5s) Options*/        /*Toast t= Toast.maketext (This, "landing success", Toast.length_short); Show Toast dialog box T.show ();*/Toast.maketext ( This, "Successful Landing", Toast.length_short). Show (); }}

Internal storage file (read)

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.