Android加载raw文件夹下的文件

来源:互联网 发布:linux root登陆 编辑:程序博客网 时间:2024/05/22 06:16

res/raw工程目录下都可以放一些文件,这些文件将被打包到APK中应用使用。这些文件不会被平台编译,而是作为可用的原始资源。
读取res/raw下的文件资源,通过以下方式获取输入流来进行写操作
InputStream is =getResources().openRawResource(R.id.filename);
使用这里面的从资源有两种方法:
第一种,将文件拷贝到data/data目录下,以(以加载数据库为例)
具体代码如下:

public class DBManager {    private final int BUFFER_SIZE = 40000;    public static final String DB_NAME = "dbName.db"; // 保存的数据库文件名(raw文件夹下数据库文件的名称)    public static final String PACKAGE_NAME = "com.example.proName"; // sd卡数据库在此位置下(修改为你项目所在的包名)    public static final String DB_PATH = "/data"            + Environment.getDataDirectory().getAbsolutePath() + "/"            + PACKAGE_NAME;    // 在手机里存放数据库的位置(/data/data/com.example.proName(你的项目的包名)/dictionarydanci.db(数据库文件名称))    private SQLiteDatabase database;    private Context context;    public DBManager(Context context) {        this.context = context;    }    public SQLiteDatabase getDatabase() {        return database;    }    public void setDatabase(SQLiteDatabase database) {        this.database = database;    }    public void openDatabase() {        System.out.println(DB_PATH);        System.out.println(DB_NAME);        System.out.println(DB_PATH + "/" + DB_NAME+"!!!!!!!!!!");        this.database = this.openDatabase(DB_PATH + "/" + DB_NAME);    }    private SQLiteDatabase openDatabase(String dbfile) {        try {            if (!(new File(dbfile).exists())) {                // 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库                InputStream is = this.context.getResources().openRawResource(                        R.raw.dictionarydanci); // 欲导入的数据库                FileOutputStream fos = new FileOutputStream(dbfile);                byte[] buffer = new byte[BUFFER_SIZE];                int count = 0;                while ((count = is.read(buffer)) > 0) {                    fos.write(buffer, 0, count);                }                fos.close();                is.close();            }            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,                    null);            return db;        } catch (FileNotFoundException e) {            Log.e("Database", "没有找到数据库。");            e.printStackTrace();        } catch (IOException e) {            Log.e("Database", "IO异常。");            e.printStackTrace();        }        return null;    }    public void closeDatabase() {        this.database.close();    }    // /////////////////////////////////////}

使用这种方法将raw文件下的数据库文件复制到/data/data/com.example.proName(你的项目的包名)/dbName.db(数据库文件名称)文件夹(也可以是你想要的任何文件夹)下
之后就可以使用常用的数据库dao类对数据库进行相关的增删改差操作
在dao类中,使用SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
DBManager.DB_PATH + “/” + DBManager.DB_NAME, null);
这条语句读取已经存在的数据库,之后使用database对数据库进行增删改差的操作
!!!:在使用之前注意判断数据库文件是否存在,同时判断数据库文件是否打开
dao类中数据库文件使用之后要注意及时的释放资源,关闭数据库文件和游标结果集合
第二种:利用缓存使用
raw下的文件不同于assert下的,raw中的文件会在R文件中生成ID,R类会自动提供该id.提速文件读取。其原理就是读的时候,先把文件的一些数据读到缓冲中。这样的好处是如果读的内容已经在缓冲中,就读缓冲的数据。如果没有,就让缓冲先从文件读取数据,然后再从缓冲读数据。
InputStream is = getResources().openRawResource(R.id.fileNameID) ;
//R.id.fileNameID为需要访问的文件对应的资源ID.接着我们就可以通过输入流来读取相应文件的内容了。

 void readRawFile()    {        String content;        Resources resources=this.getResources();        InputStream is=null;        try{            is=resources.openRawResource(R.raw.hubin);            byte buffer[]=new byte[is.available()];            is.read(buffer);            content=new String(buffer);            Log.i(tag, "read:"+content);        }        catch(IOException e)        {            Log.e(tag, "write file",e);        }        finally        {            if(is!=null)            {                try{                is.close();                }catch(IOException e)                {                    Log.e(tag, "close file",e);                }            }        }            }

实际上这两种方法没有本质的区别,归根到底都是利用 InputStream is =getResources().openRawResource(R.id.filename); 进行读取,只不过第一种是将文件拷贝的apk安装之后的文件路径下,可以在安装时就进行,安装之后就无需在raw文件下进行读取操作了。而第二种方式是将文件加载到缓存之中,随用随取。这样的好处是如果读的内容已经在缓冲中,就读缓冲的数据如果没有,就让缓冲先从文件读取数据,然后再从缓冲读数据。这样的好处是减少对文件的操作次数,从而达到提高性能的目的。坏处是要额外的内存来做缓冲区。而具体到底该使用那种方式,就得看实际访问的文件类型和大小了。

1 0
原创粉丝点击