//点击按钮 生成一个私有的文件
public void click1(View v){
try {
FileOutputStream fos = openFileOutput("private.txt", MODE_PRIVATE);
// fos.write("private".getBytes());
fos.write("haha".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮 使用MODE_APPEND模式 生成一个append.txt文件 依然是私有,可以用append模式向后添加数据而不是覆盖原文本数据
public void click2(View v){
try {
FileOutputStream fos = openFileOutput("append.txt", MODE_APPEND);
// fos.write("append".getBytes());
fos.write("hehe".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮 使用MODE_WORLD_READABLE 模式 生成一个read.txt 文件 在Android7.0版本被禁用
public void click3(View v){
try {
FileOutputStream fos = openFileOutput("read.txt", MODE_WORLD_READABLE);
fos.write("read".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮 使用这个模式MODE_WORLD_WRITEABLE 生成一个write.txt文件 在Android7.0版本被禁用
public void click4(View v){
try {
FileOutputStream fos = openFileOutput("write.txt", MODE_WORLD_WRITEABLE);
fos.write("write".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在另一个App里读取上一个App文件内的数据,在Android7.0版本被禁用
try {
File file = new File("data/data/com.example.a14_file_permission/files/write.txt");
FileOutputStream fos = new FileOutputStream(file);
BufferedWriter bufr = new BufferedWriter(new OutputStreamWriter(fos));
String content = "content";
bufr.write(content);
bufr.close(); //流用完要关闭,刚才没关闭流,在流缓冲里的content没写进去。
Log.w("JustTest", content);
} catch (Exception e) {
e.printStackTrace();
}
以上测试在Android4.0.3版本进行。