Finally, click Save to save the content to the file. Click Show to read and display the content from the file.
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "Enter the content to save: "<br/> <edittext <br/> Android: Id =" @ + ID/addtext "<br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: hint = "Enter the file content here! "<Br/> <button <br/> Android: Id =" @ + ID/addbutton "<br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "save" <br/> <button <br/> Android: Id = "@ + ID/showbutton" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "show" <br/> <textview <br/> Android: Id = "@ + ID/showtext" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </P> <p> </linearlayout> <br/>
Activity Code
Package cn.com. file; </P> <p> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> I Mport android. widget. edittext; <br/> Import android. widget. textview; <br/> Import android. widget. toast; </P> <p> public class filetest extends activity {<br/> private edittext; <br/> private textview showtextview; <br/> // name of the file to be saved <br/> private string filename = "chenzheng_java.txt "; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br /> Setcontentview (R. layout. main); <br/> // obtain the components on the page <br/> edittext = (edittext) findviewbyid (R. id. addtext); <br/> showtextview = (textview) findviewbyid (R. id. showtext); <br/> button addbutton = (button) This. findviewbyid (R. id. addbutton); <br/> button showbutton = (button) This. findviewbyid (R. id. showbutton); <br/> // bind a click event <br/> addbutton. setonclicklistener (listener); <br/> showbutton. setonclicklistener (L Istener); </P> <p >}</P> <p> // declare the listener <br/> private view. onclicklistener listener = new onclicklistener () {<br/> Public void onclick (view v) {<br/> button view = (button) V; <br/> switch (view. GETID () {<br/> case R. id. addbutton: <br/> Save (); <br/> break; <br/> case R. id. showbutton: <br/> Read (); <br/> break; </P> <p >}</P> <p> }; </P> <p>/** <br/> * @ author chenzheng_java <br/> * save user input to a file <br/> */<br/> P Rivate void save () {</P> <p> string content = edittext. gettext (). tostring (); <br/> try {<br/>/* open an output stream based on the file name and Application Mode of the file. if the file is not stored, the system will create one for you. <br/> * I am wondering why there is a filenotfoundexception throw in this place. In context, <br/> * public abstract fileoutputstream openfileoutput (string name, int mode) <br/> * throws filenotfoundexception; <br/> * openfileoutput (string name, int mode); <br/> * The first parameter indicates the file name, note that the file name here cannot contain any separator/or, only file name <br/> * the file will be saved in/data/Application name/files/chenzheng_java.txt <br/> * The second parameter, indicates the file operation mode <br/> * mode_private private (only applications that can be created for access) during Repeated writes, the file overwrites <br/> * When mode_append is private, the file is appended at the end of the file., Instead of overwriting the original file <br/> * mode_world_readable public readable <br/> * mode_world_writeable public readable and writable <br/> **/<br/> fileoutputstream outputstream = openfileoutput (filename, <br/> activity. mode_private); <br/> outputstream. write (content. getbytes (); <br/> outputstream. flush (); <br/> outputstream. close (); <br/> toast. maketext (filetest. this, "saved successfully", toast. length_long ). show (); <br/>}catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * @ author chenzheng_java <br/> * read the data saved by the user just now. <br/> */<br/> private void read () {<br/> try {<br/> fileinputstream inputstream = This. openfileinput (filename); <br/> byte [] bytes = new byte [1024]; <br/> bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream (); <br/> while (inputstrea M. Read (bytes )! =-1) {<br/> arrayoutputstream. write (bytes, 0, bytes. length); <br/>}< br/> inputstream. close (); <br/> arrayoutputstream. close (); <br/> string content = new string (arrayoutputstream. tobytearray (); <br/> showtextview. settext (content); </P> <p >}catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p>}
Others are default.
You can use the File explorer tool carried by ADT to view the file storage path. How can we call out the file explorer tool? We can see file explorer under windows -- showview -- others-android. Here is one of mine.
There is basically no difficulty with this program, that is, pure Java stream knowledge. The only difference is that context provides two methods to obtain the input and output streams. Simple, convenient, and fast.