阅读更多

Android文件保存

     一、Android的底层使用Linux内核,文件对文件所有者、与文件所有者同组的其它人、以及其它组的成员分别有可读、可写和可执行三种权限,具体可以参考《Linux私房菜》,当然,学习Android不需要了解这么多啊,在Android中,文件操作大致有四种操作模式,分别是MODE_PRIVATE、MODE_APPEND、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE等。

 

下面对于四种操作模式进行简单的介绍:

  • MODE_PRIVATE(私有操作模式):创建出来的文件只有本应用能够访问,其它应用不能访问,另外用私有操作模式生成的文件,写入文件的内容会覆盖以前已有的内容
  • MODE_APPEND(追加操作模式):创建出来的文件只有本应用能够访问,其它应用不能访问,另外用追加操作模式生成的文件,写入文件的内容会追加在以前文件的前面
  • MODE_WORLD_READABLE(可读操作模式):创建出来的文件所有应用都能够访问,另外用可读操作模式生成的文件,写入文件的内容会覆盖掉原来的内容,但是不能修改内容
  • MODE_WORLD_WRITEABLE(可写操作模式):创建出来的文件其它应用不能够访问,另外用可写操作模式生成的文件,写入文件的内容会覆盖掉原来的内容,其它应用也可以修改内容
public class FileService {private Context context = null;private static final String TAG = "FileService";public FileService(Context context) {this.context = context;}/* * 私有操作模式保存文件 */public void savePrivate(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_PRIVATE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 追加操作模式保存文件 */public void saveAppend(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_APPEND);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 可读操作模式保存文件 */public void saveReadable(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_WORLD_READABLE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 可写操作模式保存文件 */public void saveWritable(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_WORLD_WRITEABLE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}}

 二、文件的读取

读取文件的时候,如果读取文件是当前应用底下的文件可以使用Context对象来获得文件输入流,如果是读取其它目录的文件,自己创建一个文件读取流即可,读取相对比较简单,就不多做介绍。

/** * 读取文件内容 * @param fileName 文件名 * @return java.lang.String 返回文件内容 * @throws IOException 抛出的IO流异常 */public String read(String fileName) throws IOException {InputStream is = context.openFileInput(fileName);byte[] buf = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();int len = 0;while((len = is.read(buf)) != -1) {baos.write(buf , 0 , len);}is.close();byte[] res = baos.toByteArray();baos.close();return new String(res);}
/** * 读取非当前应用底下的文件 * @return java.lang.String 返回文件内容 * @throws IOException 抛出IO流异常 */public String read() throws IOException {String path = PATH + fileName;File file = new File(path);FileInputStream fis = new FileInputStream(file);ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buf = new byte[1024];int len = 0;while((len = fis.read(buf)) != -1) {baos.write(buf , 0 , len);}byte[] result = baos.toByteArray();return new String(result);}

 

三、保存文件到存储卡(简略介绍)

/** * 保存文件到存储卡设备 * @param name 要保存的文件名 * @param content 文件内容 * @throws IOException 抛出IO流异常 */public void saveSdcard(String name, String content) throws IOException {File file = new File("/mnt/sdcard/" + name);FileOutputStream fos = new FileOutputStream(file);fos.write(content.getBytes());fos.close();}

更多相关文章

  1. Android(安卓)源码下载
  2. Android(安卓)打造编译时注解解析框架 这只是一个开始
  3. Android(安卓)文件系统及权限修改
  4. Android(安卓)打包签名
  5. Android野史系列:3.利用Android(安卓)Studio开发一个demo应用
  6. Android(安卓)Studio 单刷《第一行代码》系列 01 —— 第一战 He
  7. Android(安卓)Studio 单刷《第一行代码》系列 04 —— Activity
  8. Android(安卓)系统概要 ——《第一行代码 Android》
  9. ubuntu下第一个Jni例子

随机推荐

  1. Android P Android.dp添加逻辑判断
  2. Android的帧布局
  3. android监听当前应用
  4. Android studio 常见错误以及问题
  5. android 自定义相册 多选
  6. Android版本及API等级关系
  7. java.lang.NullPointerException: Attemp
  8. Duplicate files copied (Android Studio
  9. android 操作文件
  10. Ubuntu 环境编译Kernel和Android