您的位置:首页 > 移动开发 > Android开发

android将asseet当中的数据库文件拷到程序目录

2015-04-17 18:11 363 查看
/**
* 将asseet当中的数据库文件拷到程序目录
* @param activity        当前Activity
* @param filePath        你的程序目录        getApplicationContext().getFilesDir().getAbsolutePath();
* @param fileName        你的数据库名称
*/
public static void copyEmbassy2Databases(Activity activity, String filePath, String fileName) {
System.out.println("-->copyEmbassy2Databases");
File oldfile = new File(filePath, fileName);

if (oldfile.exists())
oldfile.delete();

File file = new File(filePath, fileName);

if (file.exists())
return;

file.getParentFile().mkdirs();

InputStream in = null;
OutputStream out = null;

try {

out = new FileOutputStream(file);
byte[] buff = new byte[1024];
int len = 0;
in = activity.getAssets().open(fileName);
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
out.flush();
in.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐