【发布时间】:2015-11-14 17:01:31
【问题描述】:
我正在尝试从外部驱动器 (USB) 导入数据库文件,以恢复备份数据。文件存在且路径正确,但没有导入数据库,这是我的代码:
File usb = new File("/storage/UsbDriveA/BackupHM/database.db");
File currentDB = getDatabasePath("database.db");
FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "/storage/UsbDriveA/BackupHM/database.db";
File backupDB = new File(usb, backupDBPath);
try {
source = new FileInputStream(backupDB).getChannel();
destination = new FileOutputStream(currentDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast toast1 = Toast.makeText(getApplicationContext(), ("Restore ok"), Toast.LENGTH_SHORT);
toast1.show();
finish();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(), ("Error!"), Toast.LENGTH_LONG);
toast.show();
}
}
【问题讨论】:
-
你看到了什么失败?发布的代码是否会引发 IOException?我复制了你的代码并成功传输了一个文件。