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设备与Mac电脑之间传输文件
  2. android中文件IO
  3. Android 常用UI控件的一些属性设置(在.xml文件里进行的设置)
  4. Android应用开发Android 常见的设计模式
  5. Android多媒体开发(5)————利用Android AudioTrack播放mp3文件
  6. Android 封装的数据库管理操作类
  7. AndroiManifest.xml文件中android属性

随机推荐

  1. Android短信转发默认不需要转发号码修改
  2. android中如何在代码中直接设置View的lay
  3. mount android yaffs image on ubuntu
  4. android 学习笔记有用代码片段(2)
  5. Android 实现旋转键盘的例子
  6. Android实现CoverFlow效果三
  7. android 中对xml 进行解析
  8. Android 平台如何获取程序的版本
  9. Android实现自动定位城市并获取天气信息
  10. Android SDK Tools 19出了