The use of file in Android storage is mainly used to Openfileoutput and openfileinput two methods, the following directly with an example to illustrate.
(1) Layout file Main.xml file
<?XML version= "1.0" encoding= "Utf-8"?><Tablelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <TableRow> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "file name:"android:textsize= "20SP"/> <EditTextAndroid:id= "@+id/name"Android:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:text="" /> </TableRow> <TableRow> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Content:"android:textsize= "20SP"/> <EditTextAndroid:id= "@+id/content"Android:layout_width= "200DP"Android:layout_height= "100DP"Android:text="" /> </TableRow> <ButtonAndroid:id= "@+id/save"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Save"android:textsize= "20SP" /> <ButtonAndroid:id= "@+id/read"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "read"android:textsize= "20SP" /> <!--used to display data stored in file - <TextViewAndroid:id= "@+id/result"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text=""android:textsize= "20SP"/></Tablelayout>
(2) Service method
Package Com.yby.file;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import java.io.InputStream; Import Java.io.outputstream;public class Fileservice { //File Save public void Save (OutputStream outstream,string Content) throws ioexception{ Outstream.write (Content.getbytes ()); Outstream.close (); } The read public of the file String read (InputStream inputstream) throws ioexception{ Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); byte[] buffer = new byte[1024]; int Len; while ((Len=inputstream.read (buffer))!=-1) { baos.write (Buffer,0,len); } byte[] data = Baos.tobytearray (); Baos.close (); Inputstream.close (); return new String (data);} }
(3) Activity code implementation
PackageCom.yby.file;Importjava.io.FileNotFoundException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsactivity{PrivateEditText name,content; PrivateButton Save,read; PrivateTextView result; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Name=(EditText) Findviewbyid (r.id.name); Content=(EditText) Findviewbyid (r.id.content); Save=(Button) Findviewbyid (R.id.save); Read=(Button) Findviewbyid (R.id.read); Result=(TextView) Findviewbyid (R.id.result); Save.setonclicklistener (listener); Read.setonclicklistener (listener); } PrivateOnclicklistener listener =NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub Switch(V.getid ()) { CaseR.id.save:Try { //context.mode_private: As the default mode of operation, which means that the file is private data and can only be accessed by the app itself, in which the content of the write overwrites the contents of the original file, if you want to append the newly written content to the original file. You can use Context.mode_append//Context.mode_append: The mode checks whether the file exists, appends content to the file, or creates a new file. //context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the 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. //If you want the file to be read and written by another app, you can pass in://openfileoutput ("output.txt", context.mode_world_readable + context.mode_world_writeable);OutputStream OutStream = mainactivity. This. Openfileoutput (Name.gettext (). toString (), context.mode_append); Fileservice Service=NewFileservice (); Service.save (OutStream, Content.gettext (). toString ()); Toast.maketext (mainactivity. This, "Saved successfully", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); } Break; CaseR.id.read:Try{InputStream instream= Mainactivity. This. Openfileinput (Name.gettext (). toString ()); Fileservice Service=NewFileservice (); String Str=Service.read (instream); Result.settext (str); } Catch(Exception e) {e.printstacktrace (); } Break; default: Break; } } };}
(4) Use shell commands to view the contents of the file being written
The file for Android data storage