---restore content starts---
Storing data in a file
Package com.example.hzq_study;
Import Java.io.BufferedWriter;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import android.app.Activity;
Import Android.app.Fragment.SavedState;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.EditText;
public class Mainactivity extends Activity {
Private EditText edit;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (EditText) Findviewbyid (R.id.edit);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
String Inputtext = Edit.gettext (). toString ();
try {
Save (Inputtext);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
private void Save (String inputtext) throws IOException {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = Openfileoutput ("Data", context.mode_private);
writer = new BufferedWriter (new OutputStreamWriter (out));
Writer.write (Inputtext);
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
if (writer! = null) {
Writer.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Reading data from a file
Package com.example.hzq_study;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.Reader;
Import android.app.Activity;
Import Android.app.Fragment.SavedState;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.EditText;
Import Android.widget.Toast;
/**
* Phone Internal Storage Read file
* @author Houzhiqiang
*
*/
public class Mainactivity extends Activity {
Private EditText edit;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (EditText) Findviewbyid (R.id.edit);
String inputtext = Load ();
if (! Textutils.isempty (Inputtext)) {//Use Textutils.isempty
Edit.settext (Inputtext) when making a non-null judgment on a string;//edit_ SetText is a text that sets an edit control.
Edit.setselection (Inputtext.length ());//Set the cursor to the specified position, move the input cursor to the end of the text to facilitate the continuation of the input
Toast.maketext (this, "Restoring Succeeded ", Toast.length_short). Show ();
}
}
Public String load () {
FileInputStream inputstream = null;
BufferedReader reader = null;
StringBuilder content= new StringBuilder ();
try {InputStream = openfileinput ("Data");
InputStream in = null;
reader = new BufferedReader (new InputStreamReader (in));
String line = "";
while (line = Reader.readline ()) = null) {
Content.append (line);//The main operation on StringBuilder is the append and insert methods. Each method effectively converts the given data into a string and then adds or inserts the character of the string into the string builder. The Append method always adds these characters to the end of the generator, while the Insert method adds characters at the specified point. For example, if z refers to a string generator object with the current content of "start", the method call Z.append ("le") will cause the string generator to contain "startle"
}
}catch (IOException e) {
E.printstacktrace ();
}
finally{
if (reader! = null) {
try {
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
return content.tostring ();
}
}
Package com.atguigu.l04_datastorage;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import android.app.Activity;
Import Android.content.Context;
Import Android.content.res.AssetManager;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.Toast;
/**
* Test your phone's internal file storage
*
* @author Zhang Xiaofei
*
*/
public class Ifactivity extends Activity {
Private ImageView iv_if;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_IF);
iv_if = (ImageView) Findviewbyid (r.id.iv_if);
}
public void Save (View v) throws IOException {
1. Get inputstream--> read assets under the Logo.png
Get Assetmanager
Assetmanager manager = Getassets ();
Read file
InputStream is = Manager.open ("Logo.png");
2. Get Outputstream-->/data/data/packagename/files/logo.png
FileOutputStream fos = openfileoutput ("Logo.png", context.mode_private);
3. Write while reading
byte[] buffer = new byte[1024];
int len =-1;
while ((Len=is.read (buffer))!=-1) {
Fos.write (buffer, 0, Len);
}
Fos.close ();
Is.close ();
4. Tips
Toast.maketext (This, "save Complete", 0). Show ();
}
public void Read (View v) {///data/data/packagename/files/logo.png
1. Get the path to the picture file
/data/data/packagename/files
String Filespath = Getfilesdir (). GetAbsolutePath ();
String ImagePath = filespath+ "/logo.png";
2. Read load picture file get Bitmap object
Bitmap Bitmap = Bitmapfactory.decodefile (ImagePath);
3. Set it to show in ImageView
Iv_if.setimagebitmap (bitmap);
}
}
---restore content ends---
Phone internal file storage