/**
* 将assets文件夹下/db的本地库拷贝到/data/data/下
*
* @param context
* @param tab_name
*/
public static void copyDbFile(Context context, String tab_name) {
InputStream in = null;
FileOutputStream out = null;
/**data/data/路径*/
String path = "/data/data/" + context.getPackageName() + "/databases";
File file = new File(path + "/" + tab_name);
try {
//创建文件夹
File file_ = new File(path);
if (!file_.exists())
file_.mkdirs();
if (file.exists())//删除已经存在的
file.deleteOnExit();
//创建新的文件
if (!file.exists())
file.createNewFile();
in = context.getAssets().open("db/" + tab_name + ".db"); // 从assets目录下复制
out = new FileOutputStream(file);
int length = -1;
byte[] buf = new byte[1024];
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
本文介绍了一种方法,用于将Android应用的assets文件夹下的数据库文件复制到/data/data/目录下对应的应用数据库文件夹中。该方法首先检查目标文件夹是否存在,并创建必要的文件夹结构。随后删除已存在的旧文件,并从assets文件夹中复制新的数据库文件。
8591

被折叠的 条评论
为什么被折叠?



