Android storage learns to read and write files in internal storage

Source: Internet
Author: User

As we all know, in the actual work of development, enter the need to write a file to the phone's storage. Since we're talking about cell phone storage space, let's talk about cell phone storage space classification:

1: Internal storage space

RAM memory: The memory of the phone, which is equivalent to the memory of the computer

Rom Memory: That is, the storage memory of the phone, the equivalent of the computer's hard disk

2: External storage space

That is, pluggable SD card, equivalent to the computer's mobile hard disk, U disk and so on.


Since cell phone storage is divided into two categories, let's look at how to read and write files in the phone's internal storage.

Let's introduce an example that is very common in practice:


Generally with the login interface, you need to save the user's information to local. We use the example above to save the information entered by the user to the phone's internal storage.

The layout file code is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${relativepackage}.${activityclass}" android:orientation= "vertical" > <edittext android: Id= "@+id/ed_name" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:hi Nt= "Please enter user name" android:layout_margintop= "20DP" android:layout_marginleft= "20DP" Android:layout_marginrig        ht= "20DP"/> <edittext android:id= "@+id/ed_passwd" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter password" android:layout_marginleft= "20DP" and roid:layout_marginright= "20DP" android:inputtype= "Textpassword"/> <relativelayout and Roid:layout_width= "Fill_parent" Android:layout_height= "Wrap_content" > <checkbox android:id= "@+id/cb" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Auto Login" Andro Id:layout_centervertical= "true" android:layout_marginleft= "40DP"/> &lt ; Button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "Login" android:layout_marginright= "40DP" android:layout_alignparentright= "true" Androi d:onclick= "Login"/> </RelativeLayout></LinearLayout>


When we click on the OK button, we will write our user information to the internal storage of the phone, which is the name of the package in which the app resides.

public void Login (View v)    {    String name = Ed_nam.gettext (). toString ();        String passwd = Ed_passwd.gettext (). toString ();        Determine if the user name or password is entered if    ((Name.equals (")) | | (Passwd.equals ("")))    {    Toast.maketext (this, "User name or password cannot be empty", Toast.length_short). Show ();    else     {    //If automatic login is checked, we will need to save the user name and password    if (cb.ischecked ())    {    //Create a file, the user saves the user name and password    file file = new File ("Data/data/com.demo.storage/info.txt");    try {    FileOutputStream fos = new FileOutputStream (file);//write user name and password to name# #passwd的格式写入fos. Write ((name + "# #" + passwd). GetBytes ());//Close output stream fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}    }        Toast.maketext (This, "Login successful", Toast.length_short). Show ();}    }

The file storage path I specified is the Info.txt file under the package name:

We export can look under:



To get here, we have finished writing to the internal storage of the phone. The next is read in the internal storage of the phone.

So let's do this, when if just click into this actiivty to show the used and password for the previously saved

public void Readinfo () {File File = new file ("Data/data/com.demo.storage/info.txt");//If the file exists, read if (file.exists ()) {try {FileInputStream fin = new FileInputStream (file);//Transfer bytes to a character stream BufferedReader buffer = new BufferedReader (new InputStreamReader (Fin));//Read the user name and password in the file string text = Buffer.readline ();/with # #为关键字分割字符String s[] = Text.split ("# #");// Set into Edittexted_nam.settext (S[0]); Ed_passwd.settext (S[1]);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

As soon as we enter it will show back, I can enter the user name and password:


Our reading and writing is complete here.

But some people have not found that our path above are written by ourselves, but also by their own hand to write, in case the middle of a wrong letter, it would be very troublesome.

But Google has provided us with a dedicated access to internal storage api:getfilesdir ()

    Create a file where the user saves the user name and password    //file file = new file ("Data/data/com.demo.storage/info.txt");        Getfilesdir returns a file object whose path is: data/data/com.demo.storage/files    File File = new file (Getfilesdir (), "Info.txt") ;
The path to the Getfiledir is under the files file under the package name: When we click Login, a info.txt file is created under this path


Similarly, Google also provides a api:getcachedir (), the path you can guess, is the path of the cache.

We go into the system setup, the app is running and we find this process


As shown, where the Clear Data button is cleared for all content under the Package name folder, clear cache clears the contents of the cache folder



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

Android storage learns to read and write files in internal storage

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.