private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button btn = (Button)v;
String filename = name.getText().toString();
String content = age.getText().toString();
switch (btn.getId()) {
case R.id.save:
try {
fileService.save(filename, content);
Toast.makeText(FileActivity.this, R.string.fileSaveSuccess,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(FileActivity.this, R.string.fileSaveException,
Toast.LENGTH_LONG).show();
}
break;
case R.id.saveToSdCard:
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
fileService.saveToSd(filename, content);
Toast.makeText(FileActivity.this,
R.string.fileSaveSuccess, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(FileActivity.this, R.string.fileSaveException,
Toast.LENGTH_LONG).show();
}
} else {
Log.e("lixp", "Environment.getExternalStorageState() is not equals ..");
}
break;
case R.id.read:
//有错误5-16
FileService fileService = new FileService(getApplicationContext());
Intent intent = getIntent();
String fileName = intent.getStringExtra("fileName");
Log.d("lixp", "fileService.read(fileName) = " + fileService.read(fileName));
/*try {
content.setText(fileService.read(fileName));
}
catch(Exception e) {
e.printStackTrace();
}*/
break;
default:
break;
}
}
};
}
package com.google.datastore.filestore;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
/**
* 保存文件
*/
public void save(String fileName, String content) {
try {
//以私有方式读写数据,创建出来的文件只能被该应用访问
FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
}
catch(Exception e) {
Log.e("lixp", "save() e ============" + e);
}
}
/**
* 保存文件到sdcard
*/
public void saveToSd(String fileName, String content){
try {
// File file = new File(new File("/mnt/sdcard"), fileName);
//考虑不同版本的sdCard目录不同,采用系统提供的API获取SD卡的目录
//命名要避免冲突,和本包的冲突了
File file = new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
}
catch(Exception e) {
Log.e("lixp", "saveToSd() e =============" + e);
}
}
/**
* 读取文件内容
*/
public String read(String fileName) {
byte[] data = null;
try {
FileInputStream fileInputStream = context.openFileInput(fileName);
////把每次读取的内容写入到内存中,然后从内存中获取
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
//只要没读完,不断的读取
while((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
//得到内存中的数据 以二进制存放的
data = outputStream.toByteArray();
}
catch(Exception e) {
Log.e("lixp", "read() e ===========" + e);
}
//根据二进制数据转换成所对应的字符串
return new String(data);
}
}
4.SQLITE数据库存储
package com.google.datastore.sqllite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;