私有文件访问测试
package cn.itcast.test;import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream;import android.test.AndroidTestCase; import android.util.Log;public class AccessOtherAppPrivateTest extends AndroidTestCase {public void testAccessPrivate() throws Throwable{String path = "/data/data/cn.itcast.files/files/itcast.txt";File file = new File(path);FileInputStream inStream = new FileInputStream(file); ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len=inStream.read(buffer))!=-1){outStream.write(buffer, 0, len) ;}byte[] data = outStream.toByteArray();outStream.close();inStream.close(); String content = new String(data);Log.i("test", content);} }
扩展保存
public void saveAppend(String filename, String content) throws Exception{// TODO Auto-generated method stub// MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);outStream.write(content.getBytes());outStream.close();}
可读
public void saveReadable(String filename, String content) throws Exception{// TODO Auto-generated method stub// MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);outStream.write(content.getBytes());outStream.close();}
读写
public void saveRW(String filename, String content) throws Exception{// TODO Auto-generated method stub// MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);outStream.write(content.getBytes());outStream.close();}