我正在複製我的數據庫文件到SD卡車使用這種方法,請告訴我,如果在SD卡的文件已經存在,那麼它會替換還是不會複製?此代碼是否會用新文件替換現有數據庫文件?
public boolean copyDbToSDCard() {
boolean success = false;
String SDCardPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
final String DBPATH = SDCardPath + "/BD/";
final String DBNAME = "Mydb3.db";
this.getReadableDatabase();
File directory = new File(DBPATH);
if (!directory.exists())
directory.mkdir();
close();
try {
InputStream mInput = new FileInputStream(DB_PATH + DB_NAME);
OutputStream mOutput = new FileOutputStream(DBPATH + DBNAME);
byte[] buffer = new byte[1024];
int length;
while ((length = mInput.read(buffer)) > 0) {
mOutput.write(buffer, 0, length);
}
mOutput.flush();
mOutput.close();
mInput.close();
success = true;
} catch (Exception e) {
Toast.makeText(myContext,
"copyDbToSDCard Error : " + e.getMessage(),
Toast.LENGTH_SHORT).show();
e.fillInStackTrace();
}
return success;
}
如何測試你的代碼inst問問它是否有效?這幾乎是方式更快,不會浪費我們的時間 – WarrenFaith
我不能在地面上測試 – Trikaldarshi
「地面」是什麼意思? – WarrenFaith