我想创建一个SqliteManager类来使用Sqlite数据库。但是当我尝试复制数据库时,我收到了IO错误。这是我的代码:
try {
copyDataBase();
} catch (IOException e) {
throw new Error(e.getMessage());
}
我想在这里复制数据库。
private void copyDataBase() throws IOException {
InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
这是我的copyDatabase功能。我在这里收到错误myOutput.write(buffer, 0, length);
。
11-23 10:38:23.005: ERROR/AndroidRuntime(531): Uncaught handler: thread main exiting due to uncaught exception
11-23 10:38:23.585: ERROR/AndroidRuntime(531): java.lang.Error
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at cf.android.dbsmpl.SqliteManager$DatabaseHelper.createDataBase(SqliteManager.java:291)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at cf.android.dbsmpl.SqliteManager.open(SqliteManager.java:60)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at cf.android.dbsmpl.MainMenu.onCreate(MainMenu.java:31)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.widget.TabHost.addTab(TabHost.java:213)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at cf.android.dbsmpl.DbsmplMainActivity.onCreate(DbsmplMainActivity.java:27)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.os.Looper.loop(Looper.java:123)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-23 10:38:23.585: ERROR/AndroidRuntime(531): at dalvik.system.NativeStart.main(Native Method)
这是错误日志。当我调试此代码时,它显示此错误的原因是IOException。 我被卡住了,我希望你帮助我弄清楚问题出在哪里。感谢
答案 0 :(得分:0)
private void copyDataBase() throws IOException {
InputStream myInput = new FileInputStream("/data/data/com.dinota.emc/databases/UFS-SG.db"); //change this to your original location
String outFileName = "/data/data/com.dinota.emc/databases/" + "UFS-SG.db1"; //change this location as well
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[myInput.available()];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
试试这种方式。这没有任何问题..