您的位置:首页 > 其它

进阶之路--数据存储方式--file

2015-06-07 14:14 316 查看
文件:

1、采用私有模式创建文件:

//只能被本应用使用,私有操作模式,覆盖模式,默认路径:data/data/com.example.filesave/files
String filename_withoutpath = "hello.txt";
byte[] bytearray = filename_withoutpath.getBytes();
try {
FileOutputStream fos = context.openFileOutput(filename_withoutpath , MODE_PRIVATE);
fos.write(bytearray);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
byte[] newbytearray = new byte[1024];
ByteArrayOutputStream output = new ByteArrayOutputStream();
FileInputStream fis = context.openFileInput(filename_withoutpath);
int len = 0;
while((len = fis.read(newbytearray))!= -1){
output.write(newbytearray, 0, len);//读进内存中
}
byte[] content = output.toByteArray();
String content1 = new String(content);
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2、追加操作模式:

FileOutputStream fos = context.openFileOutput(filename_withoutpath , MODE_APPEND);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: