从资产目录里拷贝资源工具类

private void copy(String dbName) {
    //拷贝文件, 输入流-->输出流
    //输出流
    //data/data/包名/files
    File filesDir = getFilesDir();
    File desFile = new File(filesDir, dbName);//目标文件

    //数据库只需要拷贝一次
    if (desFile.exists()) {
        System.out.println( dbName + "已经存在,无需拷贝!");
        return;
    }

    AssetManager assets = getAssets();//资产目录管理器

    InputStream in = null;
    FileOutputStream out = null;
    try {
        in = assets.open(dbName);//获取资产目录文件的输入流
        out = new FileOutputStream(desFile);//输出流

        int len = 0;
        byte[] buffer = new byte[1024 * 8];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    System.out.println("拷贝" + dbName + "完成!!!");
}

 

posted on 2017-03-02 17:03  LoaderMan  阅读(172)  评论(0编辑  收藏  举报

导航