Android-读写文件总结

Android有一套自己的安全模型,具体可参见Android开发文档中文译文)。当应用程序(.apk)在安装时就会分配一个userid,当该应用要去访问其他资源比如文件的时候,就需要 userid 匹配。

默认情况下,任何应用创建的文件,数据库,sharedpreferences 都应该是私有的(位于 /data/data/包名/files/),其余程序无法访问。除非在创建时指明是 MODE_WORLD_READABLE 或者MODE_WORLD_WRITEABLE,只要这样其余程序才能正确访问。因为有这种Android读写文件的方法在安全上有所保障,进程打开文件 时Android要求检查进程的userid。所以不能直接用java的api来打开,因为java的 io函数 没有提这个机制 。

同时 android 不支持读取与工程直接子级的文件。解决办法是:在res文件夹下新建raw文件夹,或者在assets目录下,然后将文件test.xml复制到raw文件夹下。

1、必须用Andorid提供的API打开程序私有的数据,默认路径为/data/data/包名/files/
读取文件:mContext.openFileInput(fileName) ;
写入文件:mContext.openFileOutput(fileName, Context.MODE_PRIVATE) ;
MODE_PRIVATE为默认模式,代表该文件为私有数据,只能应用本身访问;
MODE_APPEND为追加模式,可以把新写入的内容追加到原文件 ;
如果希望该文件被其他应用写入可以 MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE 。
取得缓存目录:getCacheDir() 。

2、sdcard中读写非私有文件和数据需要使用java提供的API,需要在manifest.xml中增加权限:

<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

Android2.2之前,sd卡根目录路径为/sdcard ;2.2版本及之后,路径为 /mnt/sdcard/
读写之前,需要判断是否有sdcard存在: Environment.getExternalStorageState()

3、从 res/raw目录 下读取资源文件,不可写入
读取方法:mContext.getResources().openRawResource(R.raw.test);

4、从 assets目录 下读取资源文件,不可写入
读取方法:mContext.getAssets().open(fileName);

5、一个封装好的文件读写类:

package com.ppmeet;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import org.apache.http.util.EncodingUtils;
 
import android.content.Context;
 
/**
 * class name:FileService<BR>
 * class description:android文件的一些读取操作<BR>
 * PS: <BR>
 *
 * @version 1.00 2010/10/21
 * @author CODYY)peijiangping
 */
public class FileService {
    private Context context;
 
    public FileService(Context c) {
        this.context = c;
    }
 
    // 读取sd中的文件
    public String readSDCardFile(String path) throws IOException {
        File file = new File(path);
        FileInputStream fis = new FileInputStream(file);
        String result = streamRead(fis);
        return result;
    }
 
    // 在res目录下建立一个raw资源文件夹,这里的文件只能读不能写入。。。
    public String readRawFile(int fileId) throws IOException {
        // 取得输入流
        InputStream is = context.getResources().openRawResource(fileId);
        String result = streamRead(is);// 返回一个字符串
        return result;
    }
 
    private String streamRead(InputStream is) throws IOException {
        int buffersize = is.available();// 取得输入流的字节长度
        byte buffer[] = new byte[buffersize];
        is.read(buffer);// 将数据读入数组
        is.close();// 读取完毕后要关闭流。
        String result = EncodingUtils.getString(buffer, "UTF-8");// 防止乱码
        return result;
    }
 
    // 在assets文件夹下的文件,同样是只能读取不能写入
    public String readAssetsFile(String filename) throws IOException {
        // 取得输入流
        InputStream is = context.getResources().getAssets().open(filename);
        String result = streamRead(is);// 返回一个字符串
        return result;
    }
 
    // 往sd卡中写入文件
    public void writeSDCardFile(String path, byte[] buffer) throws IOException {
        File file = new File(path);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(buffer);// 也可String.getBytes()写入简单字符串;
        fos.close();
    }
 
    // 将文件写入应用的data/data的files目录下
    public void writeDateFile(String fileName, byte[] buffer) throws Exception {
        byte[] buf = fileName.getBytes("iso8859-1");
        fileName = new String(buf, "utf-8");
        FileOutputStream fos = context.openFileOutput(fileName,
                Context.MODE_APPEND);// 添加在文件后面
        fos.write(buffer);
        fos.close();
    }
 
    // 读取应用的data/data的files目录下文件数据
    public String readDateFile(String fileName) throws Exception {
        FileInputStream fis = context.openFileInput(fileName);
        String result = streamRead(fis);
        return result;
    }
}

 

 

posted on 2013-03-04 07:50  随笔x01  阅读(198)  评论(0)    收藏  举报

导航