Android Beginner Notes--read and write internal memory

Source: Internet
Author: User

The memory of the phone is divided into running memory (RAM) and the storage capacity (ROM) of the phone, of course, most of the phone also supports external memory card, today is only in the memory space of the phone for simple read and write operations

Below is a login interface and XML file content for this interface

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:gravity= "Center_vertical"6 Tools:context=". Mainactivity " >7     8     <EditText9         Android:id= "@+id/edit_name"Ten Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A Android:hint= "@string/login_username"/> -     <EditText -         Android:id= "@+id/edit_password" the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:hint= "@string/login_password" - Android:password= "true" + Android:layout_below= "@id/edit_name"/> -     <LinearLayout +         Android:layout_width= "Match_parent" A Android:layout_height= "Wrap_content" at android:orientation= "Horizontal" - Android:layout_below= "@id/edit_password"> -          -         <CheckBox -             Android:id= "@+id/choice" - Android:layout_width= "0DP" in Android:layout_height= "Wrap_content" - Android:layout_weight= "1" to Android:text= "@string/login_check"/> +         <Button -             Android:id= "@+id/btn_login" the Android:layout_width= "Wrap_content" * Android:layout_height= "Wrap_content" $ Android:text= "@string/btn_login"/>Panax Notoginseng     </LinearLayout> - </Relativelayout>

A simple relative layout page, choose whether to save the account and password, click Login, will be in the phone internal storage space to generate a corresponding file, the file type in its own discretion, here is a simple text document to save the account and password, part of the code is as follows:

 Public voidOnClick (view view) {Switch(View.getid ()) { Caser.id.btn_login:string name=Editname.gettext (). toString (); String Password=Editpassword.gettext (). toString (); if(choice.ischecked ()) {Try{File File=NewFile ("Data/data/com.zzqhfuf.innerstore/login.txt"); FOS=Newfileoutputstream (file); Fos.write ((Name+"#"+password). getBytes ());                Fos.close (); } Catch(Exception e) {e.printstacktrace (); }} toast.maketext ( This, "Landing success", 0). Show ();  Break; default:             Break; }    }

Here, when the button is clicked, a TXT file is generated in a location on the phone, the user name and password are written to the text via FileOutputStream, then the file output stream is closed, and the password and account are "#" for easy removal. The toast behind doesn't make much sense.

The next step is to read the file, and in the beginning it will read the file, so the first thing to tell is whether the file exists.

file=NewFile ("Data/data/com.zzqhfuf.innerstore/login.txt"); if(File.exists ()) {FileInputStream fis=Newfileinputstream (file); byte[] b=New byte[1024*4]; intHasread=0;                String s;  while((Hasread=fis.read (b)) >0) {s=NewString (b, 0, Hasread); String[] STRs=s.split ("#"); Editname.settext (strs[0]); Editpassword.settext (strs[1]); Choice.setchecked (true);            } fis.close (); }

Of course, the next time the user may choose not to save the password and account, but last saved the file, this will not achieve the purpose. Therefore, the saved file should be deleted when the user does not select the saved case.

if (File.exists ()) {// file exists                    if (File.isfile ()) // determine if the file is                         file.delete ();                }

Android Beginner Notes--read and write internal memory

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.