android第十步文件操作模式

Context.MODE_PRIVATE私有操作模式:创建出来的文件只能被本应用访问,其他应用无法访问该文件,另外采用私有模式创建的文件,写入文件中的内容会覆盖源文件的内容

Context.MODE_APPEND追加操作模式:只能被本应用访问,追加不覆盖内容

Context.MODE_WORLD_READBLE:可是被其他应用读取,追加不覆盖

Context.MODE_WORLD_WRITEABLE:可被其他程序文件写入数据覆盖,不可以被其他程序读取,如果想以追加的方式可以

FileOutputStream fs = new FileOutputStream(file,true); 第2个参数为是为追加

其他程序可以写也可以读

FileOutputStream  outStream = context.openFileOutput(filenametext,Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);

 

public void save(String filenametext, String filecontenttext) throws Exception { 
//文件名称千万不要带路径
//私有操作模式:创建出来的文件只能被本应用访问,其他应用无法访问该文件,
//另外采用私有模式创建的文件,写入文件中的内容会覆盖源文件的内容
FileOutputStream outStream = context.openFileOutput(filenametext,Context.MODE_PRIVATE);
outStream.write(filecontenttext.getBytes());
outStream.close();
}
//快捷键ctrl+shift+y是小写 +x是大写
public void saveAppend(String filenametext, String filecontenttext) throws Exception {

FileOutputStream outStream = context.openFileOutput(filenametext,Context.MODE_APPEND);
outStream.write(filecontenttext.getBytes());
outStream.close();
}
public void saveReadable(String filenametext, String filecontenttext) throws Exception {

FileOutputStream outStream = context.openFileOutput(filenametext,Context.MODE_WORLD_READABLE);
outStream.write(filecontenttext.getBytes());
outStream.close();
}
public void saveWriteable(String filenametext, String filecontenttext) throws Exception {

FileOutputStream outStream = context.openFileOutput(filenametext,Context.MODE_WORLD_WRITEABLE);
outStream.write(filecontenttext.getBytes());
outStream.close();
}

 

其他的测试工程

public class AccessOtherProject extends AndroidTestCase {
public void testAccessPrivate() throws Throwable{
  String path = "/data/data/com.example.file/files/text.txt"; linux完整路径
  File file =new File(path);文件对象
  FileInputStream inStream = new FileInputStream(file);放入流
  ByteArrayOutputStream array = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while((len = inStream.read(buffer))!=-1)
  {
  array.write(buffer,0,len);
  }
  byte[] data = array.toByteArray();
  array.close();
  inStream.close();
  String content = new String(data);
  Log.i("123123123", content);
  }
public void testAccessWriteable() throws Throwable{
  String path = "/data/data/com.example.file/files/writeable.txt";
  File file =new File(path);
  FileOutputStream fs = new FileOutputStream(file,true);
  fs.write("slkjflkd".getBytes());
  fs.close();
  }
}

 

 

-rw- 可以被本程序读写 rw同一组的其他应用也可以对他读写 ---其他应用不可以读写

r读 w写 第一个代表自己 第二个代表同组应用 第三个代表其他应用

上下文对象.getFileDir()获取当前应用的/file路径

上下文对象.getCacheDir()获取当前应用的/cache路径 cache 目录保存缓存

posted @ 2014-03-12 17:41  东方小花猪  阅读(330)  评论(0编辑  收藏  举报