效果图
点击保存后,创建的文件保存在/data/data/<package name>/files 目录下,可以检查一下:DDMS——File Explorer——data——data——<package name>——files——file.txt
选中文件,点击可以将文件导出,便于查看;点击可将其他文件添加进来
点击保存后,提示框显示:保存成功;点击读取后,提示框显示:文件内容:×××××
布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/name" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/content" />
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:minLines="5" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
</LinearLayout>
</LinearLayout>
业务层代码:
FileService.java
package cn.bzu.fileoperation;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
//保存数据
public void save(String fileName,String content) throws Exception{
FileOutputStream fos=context.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
}
//读取数据
public String readFile(String filename) throws IOException{
FileInputStream fis=context.openFileInput(filename);
int len=0;
byte[] buffer=new byte[1024];
ByteArrayOutputStream baos=new ByteArrayOutputStream();//往内存中输出数据
while((len=fis.read(buffer))!=-1){//如果数据量很大,第2次读取的数据有可能会把第1次读取的数据给覆盖掉
baos.write(buffer, 0, len);
}
byte[] data=baos.toByteArray();//得到内存中的数据 以二进制存放的
baos.close();
fis.close();
return new String(data);//根据二进制数据转换成所对应的字符串
}
}
MainActivity.java代码:
package cn.bzu.fileoperation;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private EditText contentText;
private Button save;
private Button read;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText) this.findViewById(R.id.name);
contentText = (EditText) this.findViewById(R.id.content);
save = (Button) this.findViewById(R.id.save);
read = (Button) this.findViewById(R.id.read);
final FileService fileService = new FileService(this);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name = nameText.getText().toString();
String content = contentText.getText().toString();
try {
fileService.save(name, content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG)
.show();
}
});
read.setOnClickListener(new OnClickListener() {
String fileContent;
@Override
public void onClick(View v) {
String name = nameText.getText().toString();
try {
fileContent = fileService.readFile(name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "文件内容:" + fileContent,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}