The Sharedpreferences object allows you to save some "key value pairs" types of data, such as user ID, birthday, gender, ID number, and so on. However, there are times when you need to use a traditional file system to save data. For example, you may want to save an article that will be displayed in your application. In the Android system, you can also use the Java.io package to implement this function.
In the Android system, the first way to save a file is to store it in an internal device. The following shows how to save the string entered in the book to an internal storage device.
1. Create a project, Files.
2. Code in the Main.xml.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertic Al "> <textview android:layout_width=" fill_parent "android:layout_height=" Wrap_ Content "android:text=" Please enter some text "/> <edittext android:id=" @+id/tx
TText1 "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "/>" <button android:id= "@+id/btnsave" android:layout_width= "Fill_parent" android:layout_height=
"Wrap_content" android:onclick= "Onclicksave" android:text= "Save"/> <button
Android:id= "@+id/btnload" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:onclick= "onClickload "android:text=" Load "/> </LinearLayout>
The code in
3. Filesactivity.java.
Package net.manoel.Files;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import NET.LEARN2DEVELOP.FILES.R;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.Toast;
public class Filesactivity extends activity {edittext TextBox;
static final int read_block_size = 100; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
TextBox = (edittext) Findviewbyid (R.ID.TXTTEXT1); public void Onclicksave (view view) {String str = Textbox.getteXT (). toString ();
try {fileoutputstream fout = openfileoutput ("Textfile.txt",
mode_world_readable);
OutputStreamWriter OSW = new OutputStreamWriter (fout);
---Write the string to the file---osw.write (str);
Osw.flush ();
Osw.close (); ---display file saved message---toast.maketext (getbasecontext (), "File saved
successfully! ", Toast.length_short). Show ();
---Clears the edittext---textbox.settext ("");
catch (IOException IoE) {ioe.printstacktrace ();
The public void Onclickload (view view) {Try {FileInputStream fIn = openfileinput ("Textfile.txt");
InputStreamReader ISR = new InputStreamReader (fIn);
char[] InputBuffer = new Char[read_block_size];
String s = "";
int charread; while ((Charread = Isr.read (inputbuffer)) >0) {//---Convert the chars to a String
---String readString = string.copyvalueof (inputbuffer, 0,
Charread);
s + + readString;
InputBuffer = new Char[read_block_size]; //---Set the edittext to the text this has been//read---TEXTBOX.S
Ettext (s);
Toast.maketext (Getbasecontext (), "File loaded successfully!", Toast.length_short). Show ();
catch (IOException IoE) {ioe.printstacktrace (); }
}
}