标签:
Android的文件读写与JavaSE的文件读写相同,都是使用IO流。而且Android使用的正是JavaSE的IO流,下面我们通过一个练习来学习Android的文件读写。
1.创建一个Android工程
Project name:File
BuildTarget:Android2.2
Application name:文件读写
Package name:com.jbridge.file
Create Activity:DateActivity
Min SDK Version:8
strings.xml文件内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">数据保存</string>
<string name="file_name">文件名</string>
<string name="file_content">文件内容</string>
<string name="button_file_save">保存</string>
<string name="button_file_read">读取</string>
<string name="file_save_success">保存文件成功</string>
<string name="file_save_failed">保存文件失败</string>
<string name="file_read_failed">读取文件失败</string>
</resources>
Android建议采用MVC开发模式,所以我们在Android应用开发中最好使用MVC设计模式。MVC设计模式使三层分离,从而很好的解耦,何乐而不为。
首先我们向工程中添加一个FileService.java:
package com.jbridge.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
public class FileService {
// Activity的父类的父类就是context,context与其他框架中的context相同为我们以供了一些核心操作工具。
private Context context;
public FileService(Context context) {
this.context = context;
}
public void saveToSDCard(String filename, String content) throws Exception{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(content.getBytes());
outStream.close();
}
}
public void save(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
然后再向工程中添加FileButtonOnClickEvent.java:
package com.jbridge.event;
import com.jbridge.file.R;
import com.jbridge.service.FileService;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FileButtonOnClickEvent implements OnClickListener {
// 通过activity获取其他控件
private Activity activity;
// 通过FileService读写文件
private FileService fileService;
// 打印信息用的标签
private static final String TAG = "FileButtonOnClickEvent";
public FileButtonOnClickEvent(Activity activity) {
this.activity = activity;
this.fileService = new FileService(activity);
}
public void onClick(View v) {
Button button = (Button) v;
switch (button.getId()) {
case R.id.bt_save:
// 获取文件名
EditText etFileNameS = (EditText) this.activity
.findViewById(R.id.et_file_name);
String fileNameS = etFileNameS.getText().toString();
// 获取文件内容
EditText etFileConS = (EditText) this.activity
.findViewById(R.id.et_file_content);
String fileContentS = etFileConS.getText().toString();
// 保存
try {
this.fileService.save(fileNameS, fileContentS);
// 在窗口中显示一个特效信息框
Toast.makeText(this.activity, R.string.file_save_success,
Toast.LENGTH_LONG).show();
Log.i(TAG, "save file success!");
} catch (Exception e) {
Toast.makeText(this.activity, R.string.file_save_failed,
Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
break;
case R.id.bt_read:
// 获取文件名
EditText etFileNameR = (EditText) this.activity
.findViewById(R.id.et_file_name);
String fileNameR = etFileNameR.getText().toString();
// 读取文件
try {
String fielContentR = this.fileService.readFile(fileNameR);
EditText etFileConR = (EditText) this.activity
.findViewById(R.id.et_file_content);
etFileConR.setText(fielContentR);
Log.i(TAG, "read file success!");
} catch (Exception e) {
Toast.makeText(this.activity, R.string.file_read_failed,
Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
break;
default:
break;
}
}
}
public void saveAppend(String filename, String content) throws Exception{// ctrl+shift+y / x
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
public void saveReadable(String filename, String content) throws Exception{// ctrl+shift+y / x
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
outStream.write(content.getBytes());
outStream.close();
}
public void saveWriteable(String filename, String content) throws Exception{// ctrl+shift+y / x
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());
outStream.close();
}
public void saveRW(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());
outStream.close();
}
public void savePRW(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE+Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
public String readFile(String filename) throws Exception{
FileInputStream inStream = context.openFileInput(filename);
byte[] data = readData(inStream);//?????????????????
return new String(data);
}
private byte[] readData(FileInputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
}
标签:
原文地址:http://www.cnblogs.com/shouce/p/5558140.html