This example describes how Android saves and reads data in file format. Share to everyone for your reference, specific as follows:
To secure the data directly in the form of a file in the device, the standard Java file input stream (FileInputStream) is obtained through the Context.openfileinput () method, through Context.openfileoutput () method to obtain standard Java file output stream (FileOutputStream)
Write data to file files
Findviewbyid (R.id.file). Setonclicklistener (New Button.onclicklistener () {
@Override public
void OnClick ( View v {
try {
//writable way to create or open Huangbiao.txt file
//the path to the file is/data/data/package name/files/huangbiao.txt
FileOutputStream fos = openfileoutput ("Huangbiao.txt", context.mode_append);
Writes a string to a file
fos.write ("Huangbiao". GetBytes ());
Close Data Flow
fos.close ();
} catch (FileNotFoundException e) {
//TODO auto-generated catch
block E.printstacktrace ();
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}}}
);
Ways to read data
Findviewbyid (R.id.read_file). Setonclicklistener (New Button.onclicklistener () {
@Override public
Void OnClick (View v) {
FileInputStream fis;
try {
//Open the file and get the InputStream object
fis = openfileinput ("Huangbiao.txt");
Available () returns the estimated space length
byte[] buffer = new byte[fis.available ()];
The content of the data stream is written into buffer
fis.read (buffer);
String AAA = new string (buffer);
System.out.println (AAA);
Fis.close ();
} catch (FileNotFoundException e) {
//TODO auto-generated catch block
e.printstacktrace ();
} catch ( IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}}}
);
For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with the Android program.