Android开发读写私有文件

来源:互联网 发布:网络更新时间 编辑:程序博客网 时间:2024/05/16 05:19
所谓私有文件,则是指程序自己能读取,而其它程序没有权限访问的文件,此文件保存在Data.app.程序包.file目录下面。
其中写文件的方法比较简单:
  private void writeFile(String fileName, String info) {
    try {
     FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
     byte[] bytes = info.getBytes();
     fout.write(bytes);
     fout.close();
    } catch (Exception err) {
    }
   }
  这样可以完成对私有文件的写,在写私有文件时使用的是openFileOutput 这个文件。
  上面对私有文件进行了写入,下面对私有文件进行读:
  private String readFile(String fileName) {
    try {
     FileInputStream fin = openFileInput(fileName);
     int length = fin.available();// 获取文件长度
     byte[] bytes = new byte[length];
     fin.read(bytes);
     return EncodingUtils.getString(bytes, ENCODING);
    } catch (Exception err) {
     return "";
    }
   }

  使用"openFileInput"来读取私有文件。

原文地址:点击打开链接

原创粉丝点击