Go from the following articles:
http://blog.csdn.net/zuolongsnail/article/details/6559338
http://blog.csdn.net/xiazdong/article/details/7687439
Http://www.cnblogs.com/feisky/archive/2011/01/05/1926177.html
1. File storage data uses IO operations in Java to save and read files, except that Android encapsulates the acquisition of input and output streams in the context class.
The created storage file is saved under the/data/data/<package Name>/files folder.
2. Operation.
Save file Contents: Get the output stream through Context.openfileoutput, the parameters are file name and storage mode respectively.
Read file contents: Gets the input stream through Context.openfileinput, the parameter is the file name.
Delete file: Context.deletefile Deletes the specified file, and the parameter is the name of the file that will be deleted.
Get list of file names: Gets an array of all file names under the files directory by using Context.filelist.
* method to get the file path:
Absolute path:/data/data/<package name>/files/filename
Context:Context.getFilesDir () can get to "/data/data/<package name>/files"
3. Four types of File save mode.
Context.mode_private is the default mode of operation, which means that the file is private and can only be accessed by the app itself, and the content written in that mode overwrites the contents of the original file.
Context.mode_append checks if a file exists, appends content to the file, or creates a new file.
Mode_world_readable indicates that the current file can be read by another application.
Mode_world_writeable indicates that the current file can be written by another application.
When using the mode, "+" can be used to select multiple modes, such as Openfileoutput (FILENAME, context.mode_private + mode_world_readable);
Simple application Example 1.
Target: When click Save, will be a specific file name and specific file content to save content, click on Read, will read the file content of a particular file display to the File content text box;
When you click Save, the effect is as follows:
Mainactivity.java
Package org.xiazdong.file; Import Java.io.ByteArrayOutputStream; Import Java.io.FileInputStream; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import android.app.Activity; Import Android.content.Context; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.EditText; public class Mainactivity extends Activity {private Button Savebutton,readbutton; Private EditText Filenameet,filecontentet; Private context context = this; Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View v) { if (V==savebutton) {String filename = Filenameet.gettext (). toString (); String filecontent = Filecontentet.gettext (). toString (); FileOutputStream out = null; try {out = Context.openfileoutput (filename,Context.mode_private); Out.write (Filecontent.getbytes ("UTF-8")); } catch (Exception e) {e.printstacktrace (); } finally{try {out.close (); } catch (Exception e) {e.printstacktrace (); }}} and else if (V==readbutton) {String filename = filenameet.get Text (). toString (); Gets the name of the file read fileinputstream in = null; Bytearrayoutputstream bout = null; Byte[]buf = new byte[1024]; bout = new Bytearrayoutputstream (); int length = 0; try {in = context.openfileinput (filename);//Get Input stream while ((Length=in.read (BUF))! =-1) {bout.write (buf,0,length); } byte[] content = Bout.tobytearray (); Filecontentet.settext (New String (content, "UTF-8")); Sets the contents of the text box to read} catch (Exception e) {e.printstacktrace (); } filecontentet.invalidate (); Refresh Screen try{in.close (); Bout.close (); } catch (Exception e) {}}}; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Savebutton = (Button) This.findviewbyid (R.id.savebutton); Readbutton = (Button) This.findviewbyid (R.id.readbutton); Filenameet = (EditText) This.findviewbyid (r.id.filename); Filecontentet = (EditText) This.findviewbyid (r.id.filecontent); Savebutton.setonclicklistener (listener); Readbutton.setonclicklistener (ListeNER); } }
2. Save the file to SDcard
If a file is large, it is not suitable for storing in the phone's storage;
If the phone exists sdcard, then the SDcard directory is the /mnt/sdcard directory;
Step 1: Set up emulator support SDcard
At this time the simulator has supported SDcard;
Step 2: Set permissions in the app
To access SDcard in the program, you need to request permission to access SDcard.
The permissions to add access to SDcard in Androidmanifest.xml are as follows:
<!--Create and delete files in SDcard permissions--
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--write data to sdcard permissions--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
To store files to SDcard, the program must first determine whether the phone is equipped with sdcard and can read and write.
Note: Access to SDcard must include access to SDcard in Androidmanifest.xml
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { File Sdcarddir = Environment.getexternalstoragedirectory ();//Get SDcard directory file saveFile = new file (Sdcarddir, "a.txt"); FileOutputStream OutStream = new FileOutputStream (saveFile); Outstream.write ("Test". GetBytes ()); Outstream.close ();} The Environment.getexternalstoragestate () method is used to get the state of the sdcard, and if the phone is loaded with sdcard and can read and write, the method returns a state equal to Environment.media_ Mounted. The Environment.getexternalstoragedirectory () method is used to get the directory of the SDcard, of course, to get the SDcard directory, you can also write: file Sdcarddir = new file ("/sdcard "); Get sdcard directory file SaveFile = new file (Sdcarddir, "itcast.txt"); The above two lines of code can be synthesized one sentence: file SaveFile = new file ("/sdcard/a.txt"); FileOutputStream OutStream = new FileOutputStream (saveFile); Outstream.write ("Test". GetBytes ()); Outstream.close ();
How files in Android store data