android基础---使用文件进行数据存储

来源:互联网 发布:施工企业会计软件 编辑:程序博客网 时间:2024/05/01 20:33
 

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND

Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。

MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。

如果希望文件被其他应用读和写,可以传入:

openFileOutput("xxx.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

测试:建立两个工程file与test

 

File中的service层:

package cn.com.service;

public class FileService {

    private Context context;

    public FileService(Context context){

       this.context = context;

    }

    /**

     * 保存文件

     */

    public void save(String filename, String filecontent)throws IOException {

       //私有操作模式,创建出来的文件只能被本应用访问,其他应用无法访问该文件,另外采用私有模式创建的文件将覆盖原文件

       //android系统-->(保存)sdcard,对系统来说用输出流

       FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);

       outputStream.write(filecontent.getBytes());

       outputStream.close();

    }

    public String read(String filename) throws IOException{

       //sdcard-->android系统,对系统来说用输入流

       FileInputStream inputStream = context.openFileInput(filename);

       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

       byte[] buffer = new byte[1024];

       int len = 0;

       while((len = inputStream.read(buffer))!=-1){

           outputStream.write(buffer, 0, len);

       }

       byte[] data = outputStream.toByteArray();

       return new String(data);

    }

    /**

     * Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

     */

    public void saveAppend(String filename, String filecontent)throws IOException {

       FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_APPEND);

       outputStream.write(filecontent.getBytes());

       outputStream.close();

    }

    /**

     * Context.MODE_READABLE:表示当前文件可以被其他应用读取件。

     */

    public void saveReadable(String filename, String filecontent)throws IOException {

       FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);

       outputStream.write(filecontent.getBytes());

       outputStream.close();

    }

    /**

     * MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。。

     */

    public void saveWriteable(String filename, String filecontent)throws IOException {

       FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);

       outputStream.write(filecontent.getBytes());

       outputStream.close();

    }

    /**

     * Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用读写。

     */

    public void saveReadableAndWriteable(String filename, String filecontent)throws IOException {

       FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);

       outputStream.write(filecontent.getBytes());

       outputStream.close();

    }

}

File中的测试类,来生成另一应用用来测试的文件:

package cn.com.test;

public class FileServicetest extends AndroidTestCase{

    private static final String TAG = "FileServicetest";

    public void testRead() throws Throwable{

       //AndroidTestCase提供了this.getContext(),这个方法提供了取得上下文对象

       FileService service = new FileService(this.getContext());

       String result = service.read("abc.txt");

       //通过log打印到控制台上

       Log.i(TAG,result);

    }

    /**

     * 测试利用append追加模式生成文件append.txt,供cn.itcast.other应用读取

     * @throws Throwable

     */

    public void testsaveAppend() throws Throwable{

       FileService service = new FileService(this.getContext());

       service.saveAppend("append.txt", ",追加模式");

    }

    /**

     * 测试利用Readable模式生成文件Readable.txt,供cn.itcast.other应用读取

     * @throws Throwable

     */

    public void testsaveReadable() throws Throwable{

       FileService service = new FileService(this.getContext());

       service.saveReadable("Readable.txt", "Readable");

    }

    /**

     * 测试利用Readable模式生成文件Readable.txt,供cn.itcast.other应用读取

     * @throws Throwable

     */

    public void testsaveWriteable() throws Throwable{

       FileService service = new FileService(this.getContext());

       service.saveWriteable("Writeable.txt", "Writeable");

    }

    /**

     * 测试利用Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE模式生成文件rw.txt,供cn.itcast.other应用使用(读与写)

     * @throws Throwable

     */

    public void testsaveReadableAndWriteable() throws Throwable{

       FileService service = new FileService(this.getContext());

       service.saveReadableAndWriteable("rw.txt", "rw");

    }

}

项目Other中的测试结果:

package cn.com.test;

 

public class AccessOtherAppFile extends AndroidTestCase {

    //抽取出读方法

    private static String read(InputStream inputStream)throws Exception{

       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

       byte[] buffer = new byte[1024];

       int len = 0;

       while ((len = inputStream.read(buffer)) != -1) {

           outputStream.write(buffer, 0, len);

       }

       byte[] data = outputStream.toByteArray();

       return new String(data);

    }

    /**

     * Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容

     * 测试读取cn.com.file应用中用Context.MODE_PRIVATE创建的文件abc.txt的时候,由于设置的为Context.MODE_PRIVATE模式,不能被该应用访问,报错

     * :java.io.FileNotFoundException: /data/data/cn.com.files/files/abc.txt(Permission denied) at

     */

    public void testAccessFile() throws Throwable {

       String path = "/data/data/cn.com.files/files/abc.txt";

       // 构建文件对象

       File file = new File(path);

       // 构建文件输入流对象

       FileInputStream inputStream = new FileInputStream(file);

       Log.i("AccessOtherAppFile", new String(read(inputStream)));

    }

    /**

     * Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。只能被应用本身访问。

     * 测试读取cn.com.file应用中用Context.MODE_APPEND创建的文件append.txt的时候,不能被该应用访问,报错同上

     */

    public void testAccessFile2() throws Throwable {

       String path = "/data/data/cn.com.files/files/append.txt";

       File file = new File(path);

       FileInputStream inputStream = new FileInputStream(file);

       Log.i("AccessOtherAppFile", new String(read(inputStream)));

    }

    /**

     * MODE_WORLD_READABLE:表示当前文件可以被其他应用读取

     * 测试读取cn.com.file应用中用Context.MODE_READABLE创建的文件Readable.txt的时候,能被该应用访问

     */

    public void testAccessFile3() throws Throwable {

       String path = "/data/data/cn.com.files/files/Readable.txt";

       File file = new File(path);

       FileInputStream inputStream = new FileInputStream(file);

       Log.i("AccessOtherAppFile", new String(read(inputStream)));

    }

    /**

     * MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入

     * 测试读取cn.com.file应用中用MODE_WORLD_WRITEABLE创建的文件Readable.txt的时候,不能被访问

     */

    public void testAccessFile4() throws Throwable {

       String path = "/data/data/cn.com.files/files/Writeable.txt";

       File file = new File(path);

       FileInputStream inputStream = new FileInputStream(file);

       Log.i("AccessOtherAppFile", new String(read(inputStream)));

    }

    /**

     * MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入

     * 下面用写方法测试

     */

    public void testAccessFile5() throws Throwable {

       String path = "/data/data/cn.com.files/files/Writeable.txt";

       File file = new File(path);

       FileOutputStream outputStream = new FileOutputStream(file);

       outputStream.write("abcdefghijklmn".getBytes());

       outputStream.close();

    }

    /**

     * Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用读写

     * 测试能够读取cn.com.file应用中创建的rw.txt

     */

    public void testAccessFile6() throws Throwable {

       String path = "/data/data/cn.com.files/files/rw.txt";

       File file = new File(path);

       FileInputStream inputStream = new FileInputStream(file);

       Log.i("AccessOtherAppFile", new String(read(inputStream)));

    }

    /**

     * Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用读写

     * 测试能够对cn.com.file应用中创建的rw.txt进行写操作

     */

    public void testAccessFile7() throws Throwable {

       String path = "/data/data/cn.com.files/files/rw.txt";

       File file = new File(path);

       FileOutputStream outputStream = new FileOutputStream(file);

       outputStream.write("abcdefghijklmnaaaaaaa".getBytes());

       outputStream.close();

    }

}

原创粉丝点击