2014-04-06 23 views
-1

刪除數據庫中,我用下面的代碼的Android源碼包中

String destDir = "/data/data/" + getPackageName() + 
      "/databases/"; 
    String destPath = destDir + "jobs"; 
    File f = new File(destPath); 
    if (!f.exists()) { 
     //---make sure directory exists--- 
     File directory = new File(destDir); 
     directory.mkdirs(); 
     //---copy the db from the assets folder into 
     // the databases folder--- 
     try { 
      CopyDB(getBaseContext().getAssets().open("jobs"), 
        new FileOutputStream(destPath)); 
     } catch (FileNotFoundException e) {  
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
public void CopyDB(InputStream inputStream, OutputStream outputStream) 
throws IOException { 
    //---copy 1K bytes at a time--- 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = inputStream.read(buffer)) > 0) { 
     outputStream.write(buffer, 0, length); 
    } 
    inputStream.close(); 
    outputStream.close(); 
} 

此副本從資產數據庫文件夾到數據/數據的數據庫文件夾,然後我可以使用它,但只有當它不存在。

我需要能夠刪除的文件包中,然後將文件從資產每次應用程序運行復制。

我通過複製一個新的數據庫注入資產每次夾更新數據庫中的數據,除非我完全取消安裝應用程序,並重新安裝它沒有添加新的數據。

我想最終從一個文件夾加載數據庫的設備上,這樣所有的用戶做到及時更新是替換文件和應用程序在新文件中讀取但那是以後。

回答

0

所以...只是刪除這一行:

if (!f.exists()) { 

及其相應的右括號(})

這將始終複製分貝,當你運行你的應用程序。
(它含蓄地爲你刪除的文件,一旦發現)

+0

非常感謝你 – user3422687