文件:
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);