手机内部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();
}
}
}
}
从文件中读取数据
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;
/**
* 手机内部存储 读取文件
* @author 侯志强
*
*/
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)){//对字符串进行非空判断的时候使用TextUtils.isEmpty
edit.setText(inputText);//Edit_SetText是一个设置一个编辑控件的文本。
edit.setSelection(inputText.length());//设置光标到指定位置 ,将输入光标移动到文本的末尾位置以便于继续输入
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);//在 StringBuilder 上的主要操作是 append 和 insert 方法。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符添加或插入到字符串生成器中。append 方法始终将这些字符添加到生成器的末端;而 insert 方法则在指定的点添加字符。 例如,如果 z 引用一个当前内容为“start”的字符串生成器对象,则该方法调用 z.append("le") 将使字符串生成器包含“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;
/**
* 测试手机内部文件存储
*
* @author 张晓飞
*
*/
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. 得到InputStream-->读取assets下的logo.png
//得到AssetManager
AssetManager manager = getAssets();
//读取文件
InputStream is = manager.open("logo.png");
//2. 得到OutputStream-->/data/data/packageName/files/logo.png
FileOutputStream fos = openFileOutput("logo.png", Context.MODE_PRIVATE);
//3. 边读边写
byte[] buffer = new byte[1024];
int len = -1;
while((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
//4. 提示
Toast.makeText(this, "保存完成", 0).show();
}
public void read(View v) {// /data/data/packageName/files/logo.png
//1. 得到图片文件的路径
// /data/data/packageName/files
String filesPath = getFilesDir().getAbsolutePath();
String imagePath = filesPath+"/logo.png";
//2. 读取加载图片文件得到bitmap对象
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
//3. 将其设置到imageView中显示
iv_if.setImageBitmap(bitmap);
}
}
---恢复内容结束---