/**
* 拷贝数据库
* 将assets目录下的数据库拷贝到
* "data/data/com.itheima.mobilesafe/files/address.db";
* context.getFilesDir()相当于得到
* data/data/com.itheima.mobilesafe/files
*/
public static void copyDB(final Context context) {
new Thread(){
public void run() {
InputStream is = null;
FileOutputStream fos = null;
try {
File file = new File(context.getFilesDir(), "address.db");
if(file.exists() && file.length()>0){
//已经拷贝过来 ,不需要拷贝了
System.out.println(context.getFilesDir()+"已经拷贝了 不需要了");
return;
}
is = context.getAssets().open("address.db");
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!= -1){
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
if(fos != null){
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
将工程中的数据库文件拷贝到sdcard卡中
最新推荐文章于 2021-05-27 07:57:55 发布