Android file storage and Reading

Source: Internet
Author: User

I. Permissions ISSUES

The storage space in the phone is divided into ROM and sdcard,rom with the operating system and the apps we install, and the data generated by our app is generally placed in sdcard. Of course, Android also creates a data storage space for each app in the ROM, with the specific directory under/data/data. In real-Time debugging, this directory does not have any permissions on other users, so we cannot open the directory in DDMS.

The solution is that we log on to our phone via adb, and then switch directly to the root user, and then the phone may ask for authorization, we choose to allow. This will make us the root user. Then we are changing the permissions on the/data directory so that we can see the contents of/data on the DDMS. The specific operation is as follows:

Second, layout settings
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context= "Xidian.dy.com.chujia.MainActivity">    <EditTextAndroid:id= "@+id/username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name"/>    <EditTextAndroid:id= "@+id/password"Android:inputtype= "Textpassword"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password"/>    <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <CheckBoxAndroid:id= "@+id/remember"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Remember user name and password"android:layout_centervertical= "true"/>        <ButtonAndroid:id= "@+id/login"Android:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Login"/>    </Relativelayout></LinearLayout>
Third, Java code
 PackageXidian.dy.com.chujia;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.util.Log;ImportAndroid.util.PrintWriterPrinter;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;Importjava.util.List; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button BT=(Button) Findviewbyid (R.id.login); Bt.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {EditText etusername=(EditText) Findviewbyid (r.id.username); String username=Etusername.gettext (). toString (); EditText Etpassword=(EditText) Findviewbyid (R.id.password); String Password=Etpassword.gettext (). toString (); LOG.E ("Login", "Logon succeeded"); CheckBox CB=(CheckBox) Findviewbyid (R.id.remember); if(cb.ischecked ()) {String path= "/data/data/xidian.dy.com.chujia/info.txt"; File File=NewFile (path); PrintWriter PW=NULL; Try{PW=NewPrintWriter (NewBufferedWriter (NewOutputStreamWriter (Newfileoutputstream (file))); } Catch(FileNotFoundException e) {LOG.E ("Mainactivity", "File not Found");                    } pw.println (username);                    PW.PRINTLN (password);                Pw.close ();    }            }        }); }}

Android creates a folder under/data/data for each app, and the name of the folder is the app's package name. Our app is Xidian.dy.com.chujia, so in order to save the user data, I create a infro.txt folder in/data/data/xidian.dy.com.chujia/and then save the user name and password package to that folder. The user name and password are saved using the normal Java IO stream.

Android file storage and Reading

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.