打开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="@string/filename" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/filename" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/file_content" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/save_file_content" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save"
android:id="@+id/button_save"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_read"
android:id="@+id/button_read"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/read_file_content" />
</LinearLayout><resources>
<string name="app_name">File</string>
<string name="action_settings">Settings</string>
<string name="filename">文件名称</string>
<string name="file_content">文件内容</string>
<string name="button_save">保存</string>
<string name="save_success">保存成功</string>
<string name="save_fail">保存失败</string>
<string name="button_read">读取</string>
<string name="read_success">读取成功</string>
<string name="read_fail">读取失败</string>
</resources>
public class FileService {
public Context context;
public FileService(Context context){
this.context = context;
}
/**
* 保存字符串到文件
* @param name 文件名
* @param content 文件内容
*/
public void save(String name, String content) throws Exception{
// MODE_PRIVATE 创建出来的文件,仅能被本应用访问,而且新写入的内容会覆盖原来的内容
FileOutputStream os = context.openFileOutput(name, Context.MODE_PRIVATE);
// 默认保存在/data/data/<package_name>/files目录
os.write(content.getBytes());
os.close();
}
/***
* 读取文件内容
* @param name 文件名
* @return
* @throws Exception
*/
public String read(String name) throws Exception{
FileInputStream is = context.openFileInput(name);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while( (len = is.read(buf)) != -1){
os.write(buf,0, len);
}
byte[] data = os.toByteArray();
String content = new String(data);
return content;
}
} public EditText nameText;
public EditText saveContentText;
public EditText readContentText;
public Button button_read;
public String filename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText)this.findViewById(R.id.filename);
saveContentText = (EditText)this.findViewById(R.id.save_file_content);
readContentText = (EditText)this.findViewById(R.id.read_file_content);
Button button_save = (Button) this.findViewById(R.id.button_save);
button_save.setOnClickListener(new ButtonSaveClickListener());
button_read = (Button) this.findViewById(R.id.button_read);
button_read.setClickable(false);
button_read.setOnClickListener(new ButtonReadClickListener());
}
private final class ButtonSaveClickListener implements View.OnClickListener{
public void onClick(View v){
String name = nameText.getText().toString();
String content = saveContentText.getText().toString();
FileService service = new FileService(getApplicationContext());
try {
filename = name;
service.save(name, content);
filename = name;
button_read.setClickable(true);
Toast.makeText(getApplicationContext(),R.string.save_success, Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(getApplicationContext(),R.string.save_fail, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
private final class ButtonReadClickListener implements View.OnClickListener{
public void onClick(View v){
FileService service = new FileService(getApplicationContext());
try {
String content = service.read(filename);
readContentText.setText(content);
Toast.makeText(getApplicationContext(),R.string.read_success, Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(getApplicationContext(),R.string.read_fail, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
原文地址:http://blog.csdn.net/xufeng0991/article/details/44042839