Android Learning Summary-file storage

Source: Internet
Author: User

Actions for file storage in Android:

The 1.Activity openfileoutput () method can output data to a file
2. Created files are saved in the/data/data/<package name>/files directory
3. The implementation process is the same as saving data to a file in Java

<!--Create and delete files in SD card permissions--
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--Write data permissions to the SD card--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

 Public classMainactivityextendsactivity{protected voidoncreat (Bundle savedinstancestate) {Super. Oncreat (savedinstancestate);        Setcontentview (R.layout.activity_main); File File=NewFile ("/mnt/sdcard/test");//Create a file        if(!file.exists ()) {//determine if a file exists            Try{file.createnewfile ();//Create a file if it does not exist}Catch(IOException e) {e.printstacktrace (); }        }Else{toast.maketext (mainactivity). This, "File already exists", 1000). Show (); }       //File.delete ();//Deleting Files    }}

1. Directory structure. This.getfilesdir (). Default data store directory for the current program
2.this.getcachedir (), the default cache file location, less important to create and use, when the memory is low, the system will automatically delete the cache directory data in the app
3.this.getdir ("Xch", mode_private);/data/data/< package name >/app_xch
4.this.getexternalcachedir (), the external storage location, the app will be automatically deleted after uninstall.
5. If the developer does not comply with the data/data/< package name > or/mnt/sdcard/android/data/< package name, the uninstall will not be erased and will result in so-called data garbage.

Demo:

 PackageCom.example.filereadwrite;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener {PrivateEditText content; PrivateButton Btn_saveread; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Content=(EditText) Findviewbyid (r.id.et); Btn_saveread=(Button) Findviewbyid (R.id.btn_saveread); TV=(TextView) Findviewbyid (r.id.tv); Btn_saveread.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn_saveread:writefile (Content.gettext (). toString ());                Tv.settext (ReadFile ());  Break; }    }    /*** Write content to file *@paramcontent*/     Public voidWriteFile (String content) {Try{FileOutputStream fos=openfileoutput ("Text.txt", mode_private);            Fos.write (Content.getbytes ());        Fos.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** read out the contents of the file*/     Publicstring ReadFile () {string content=""; Try{FileInputStream fis=openfileinput ("Text.txt"); Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();//can capture data from a memory buffer and convert it into a byte array            intLen=0; byte[] buffer=New byte[1024];  while(Len=fis.read (buffer))!=-1) {baos.write (buffer,0, Len); } content=baos.tostring ();            Fis.close ();        Baos.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returncontent; }}

Note: FileOutputStream fos= openoutoput (file name, access rights);
Fos.write (binary file written);

byte [] buffer = new byte[1024];//can save up to 1024 bytes

Baos.write (buffer, 0, Len);//0 indicates an offset of 0

Android Learning Summary-file 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.