从资产目录里拷贝资源工具类
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 + "完成!!!");
}
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
浙公网安备 33010602011771号