首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 读写资料数据

2012-07-19 
android 读写文件数据android 读写文件数据 Demo:public class HolderFile {final String sFileNameandr

android 读写文件数据
android 读写文件数据 Demo:

public class HolderFile {

final String sFileName="android_info.csv";

//创建并写入
public void createFile(String info) {

File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);

try {
FileWriter Writer = new FileWriter(file);
//String str = "FileDemo";
Writer.write(info);
Writer.flush();
Writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

//读取资料
public String read() {

String content = null;
File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);
try {
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
byte[] data = baos.toByteArray();
baos.close();
fis.close();
content = new String(data);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return content;

}

public boolean isFileExist() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.exists();
}

public boolean delFile() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.delete();
}

}

热点排行