Android学习笔记---文件的操作模式,各种模式之间的关系,以及沿用linux系统的安全性
.Context.MODE_WORLD_WRITEABLE模式创建的文件不可以被其他应用读取,但是可以被其他
应用写入:
-----------------------
测试代码:
public void testAccessReadWriteable() throws Throwable{
String TAG="testAccessAppend";
String path="/data/data/com.credream.file/files/writeable.txt";
File file=new File(path);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
FileInputStream inputStream=new FileInputStream(file);
byte[] buffer=new byte[1024];
int len=0;
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
byte[] data=outputStream.toByteArray();
outputStream.close();
inputStream.close();
String content=new String(data);
Log.i(TAG, content);
}
}
--------------------------------------
选中该方法,然后右键运行出现错误,提示没有权限
---------------------------------------------------
2.如果希望文件被其他应用读和写,可以传入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);//这里也可以直接写3,因为前面的变量代表的整型值是1后面的是//2
--------------------------------
FileOutputStream outStream=context.openFileOutput
(filename,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
--------------------------------------------
测试代码,用于存储文件
com.credream.service
FileService.java
public void saveRW(String filename, String content) throws Exception
{
//IO j2ee
FileOutputStream outStream=context.openFileOutput
(filename,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
outStream.write(content.getBytes());//content.getBytes()这个方法
调用系统的
outStream.close();
}
-------------------------------------------
调用FileService.java--->saveRW方法,保存文件
/File/src/com/credream/file/readTest.java
public void testRW()throws Exception{
FileService service=new FileService(this.getContext());
service.saveRW("rw.txt","rw");
}
----------------------------------------
测试即可读又可写文件:rw.txt是不是可以读取:
/OherFile/src/com/credream/othertest/AccessOtherPrivateTest.java
public void testAccessRW() throws Throwable{
String TAG="testAccessAppend";
String path="/data/data/com.credream.file/files/rw.txt";
File file=new File(path);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
FileInputStream inputStream=new FileInputStream(file);
byte[] buffer=new byte[1024];
int len=0;
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
byte[]