The final effect diagram, click Save will be saved to the file, click Show will read out the contents from the file and display.
Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <textview android:layout_width= "fill_parent" android:layout_height= wrap_content "android:text=" Please enter What to Save: "/> <edittext android:id=" @+id/addtext "android:layout_width=" Fill_parent "Android:layout_hei" ght= "wrap_content" android:hint= "Please enter the contents of the file here!" "/> <button android:id=" @+id/addbutton "android:layout_width=" Wrap_content "android:layout_height=" "Wrap_content" android:text= "save"/> <button android:id= "@+id/showbutton" android:layout_width= Ontent "android:layout_height=" Wrap_content "android:text=" show "/> <textview android:id=" @+id/showTex T "android:layout_width=" fill_parent "android:layout_height=" wrap_content "/> </lineArlayout>
Activity Code
Package cn.com.file;
Import Java.io.ByteArrayOutputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Filetest extends activity {private EditText edittext;
Private TextView Showtextview;
The filename to be saved private String filename = "Chenzheng_java.txt";
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Gets the component in the page EditText = (edittext) Findviewbyid (R.id.addtext);
Showtextview = (TextView) Findviewbyid (R.id.showtext);
Button AddButton = (button) This.findviewbyid (R.id.addbutton); Button Showbutton = (BuTton) This.findviewbyid (R.id.showbutton);
Binding order Addbutton.setonclicklistener (listener);
Showbutton.setonclicklistener (listener);
}//Declaration listener Private View.onclicklistener Listener = new Onclicklistener () {public void OnClick (View v) {
Button view = (button) v;
Switch (View.getid ()) {case R.id.addbutton:save ();
Break
Case R.id.showbutton:read ();
Break
}
}
}; /** * @author Chenzheng_java * Save user input to file */private void Save () {String content = Edittext.gettext
(). toString (); try {*/* opens an output stream based on the file name provided by the user and the application mode of the file. File does not save the system will create one for you, * As for why this place and filenotfoundexception throw, I am also more puzzled. This is defined in the context of * public abstract FileOutputStream openfileoutput (String name, int mode) * Throws Fileno
Tfoundexception;
* Openfileoutput (String name, int mode); * The first parameter, representing the file name, note that the file name here cannot include any/or/this delimiter, only the filename * The file is saved in the/data/data/application name/files/chenzheng_java.txt * The second parameter, which represents the file's operating mode * Mode_private Private (can only create its application access) heavy File overwrite on duplicate write * Mode_append Private repeat writes are appended to the end of the file instead of overwriting the original file * mode_world_readable public readable * Mode_world_writeable Public writable */FileOutputStream OutputStream = Openfileoutput (FileName, Activ ity.
Mode_private);
Outputstream.write (Content.getbytes ());
Outputstream.flush ();
Outputstream.close ();
Toast.maketext (Filetest.this, "Save Success", Toast.length_long). Show ();
catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace (); }/** * @author Chenzheng_java * Read what the user has just saved * * private void Read () {try {FILEINP
Utstream InputStream = This.openfileinput (fileName);
byte[] bytes = new byte[1024];
Bytearrayoutputstream Arrayoutputstream = new Bytearrayoutputstream (); while (Inputstream.read (bytes)!=-1) {arrayoutputstream.write (bytes, 0, bytes.length);
} inputstream.close ();
Arrayoutputstream.close ();
String content = new String (Arrayoutputstream.tobytearray ());
Showtextview.settext (content);
catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}
}
}
The rest is the default.
The path to the file save can be viewed through the File Explorer tool that ADT carries. How to pull up the File Explorer tool; We can see the file Explorer under Windows--showview--others-android. Here is a screenshot of me.
For this program, there is basically no difficulty, is the pure Java streaming knowledge. The only difference is that the context provides us with two ways to get the input output stream. Simple, convenient, fast ah.