Description
File save path is android default path,/data/app package name (full name)/files
First, activity
Package Com.example.filerw;import Com.example.server.fileserver;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.widget.EditText;import Android.widget.textview;public class Mainactivity extends Activity {private EditText contentedit;private String file_ Name= "file.txt";p rivate fileserver fileserver;private TextView showtext;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ContentEdit = ( EditText) Findviewbyid (r.id.content) Showtext = (TextView) Findviewbyid (r.id.showtext); fileserver = new Fileserver ( Getapplicationcontext ());} public void Mainclick (View v) {switch (V.getid ()) {case R.id.save:string str = Contentedit.gettext (). toString (); Fileserver.save (file_name, str); Break;case r.id.read:string show = Fileserver.read (file_name); Showtext.settext (show ); break;default:break;}} Menu public boolean oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate(R.menu.main, menu); return true;}} Second, the main realizationCode
Package Com.example.server;import Java.io.bytearrayoutputstream;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Org.apache.http.util.encodingutils;import Android.content.context;public class Fileserver {private Context context;private FileInputStream in;private FileOutputStream Out;public Fileserver (context context) {This.context = context;} /** * Save * @param name */public void Save (String name,string content) {try {out = Context.openfileoutput (name, Context.mode _private); Out.write (Content.getbytes ()); Out.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Exception e) {System.out.println (e.tostring ());}} /** * Read File * @param name * @return */public string Read (string name) {String file_content = null;try {Bytearrayoutputstream Byteout = new Bytearrayoutputstream (); in = Context.openfileinput (name);//Open file with buffer instance byte[] buf = new Byte[1024];while (( In.read (BUF))!=-1) {byteout.write (BUF);//Writes the read-out data to the cache}file_content = EncOdingutils.getstring (Byteout.tobytearray (), "GBK");//Transcode Byteout.close (); In.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Exception e) {System.out.println (e.tostring ());} return file_content;}}Third, XML
<relativelayout 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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:padding=" 0DP "> <edittext android:id=" @+id/content "android:inputtype=" text "Android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:hint=" Please enter the content " Android:textsize= "16sp" android:background= "@drawable/edit_xmlbg" android:minheight= "30DP"/> <textview android:id= "@+id/showtext" android:layout_width= "fill_parent" android:layout_height= "WR Ap_content "android:layout_below=" @+id/contenT "/> <button android:id=" @+id/save "android:onclick=" Mainclick "Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18SP" android:text= "Save" Android:layout_alignparentbottom= "true"/><button android:id= "@+id/read" android:onclick= " Mainclick "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:textSize= "18SP" android:text= "read" android:layout_alignparentbottom= "true" android:layout_torightof= "@+id/sav E "/></relativelayout>
//
Implementation of the read and write of the android file