您好我正在尝试在我的应用程序中导入数据库。但是它失败了,我收到了fileNotFountException。请帮我在我的应用程序中导入数据库。我的设备内存中我的数据库备份成功。为什么我get filenotfoundexception.I在dbname.db
中存储了我的db备份 private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
System.out.println("KKKKKKKKKKKKKKKKKKKKKK");
String currentDBPath = "//data//" + getPackageName() + "//databases//" + "dbname.db" ;
String backupDBPath = "Restore.db"; // From SD directory.
System.out.println("HHHHHHHHHHHHHHHHHHHHHHH");
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAaa");
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFFH");
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Import Successful!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
我的例外
java.io.FileNotFoundException: /data/data/com.example.rr.remainder/databases/dbname.db: open failed: ENOENT (No such file or directory)
09-22 06:02:36.486 21445-21445/com.example.rr.remainder W/System.err: at libcore.io.IoBridge.open(IoBridge.java:409)
09-22 06:02:36.486 21445-21445/com.example.rr.remainder W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:78)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at com.example.rr.remainder.restore.importDB(restore.java:84)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at com.example.rr.remainder.restore.access$100(restore.java:15)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at com.example.rr.remainder.restore$2.onClick(restore.java:35)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.view.View.performClick(View.java:4741)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.view.View$PerformClick.run(View.java:19384)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.os.Looper.loop(Looper.java:146)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5679)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at dalvik.system.NativeStart.main(Native Method)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at libcore.io.Posix.open(Native Method)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: at libcore.io.IoBridge.open(IoBridge.java:393)
09-22 06:02:36.496 21445-21445/com.example.rr.remainder W/System.err: ... 15 more
答案 0 :(得分:0)
当您尝试创建&#34; dbname.db&#34; 文件时,看起来不存在子目录 / databases 。
考虑使用以下代码,该代码将自动在应用程序的沙箱中创建一个子目录(如果它尚不存在):
public void copyBackupDb(){
File subDirPath = mContext.getDir("databases", Context.MODE_PRIVATE);
File currentDBFile = new File(subDirPath , "dbname.db");
File sd = Environment.getExternalStorageDirectory();
File backupDBFile = new File(sd,"Restore.db");
try {
copy(backupDBFile, currentDBFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
}
* mContext是Andriod活动或服务的上下文参考。