2014-08-28 52 views
3

我的程序應該工作如下:不能老是創建和複製文件

1.Copy the new database in the program folder 
2.Import the records from the old database to the new one. 

但我由於某種原因,得到一個異常。爲什麼?

protected Object doInBackground(Object[] objects) { 

     String LOCAL_DATABASE_PATH = getApplicationInfo().dataDir + File.separator + 
       "databases" + File.separator; 


     File fileDir = new File(LOCAL_DATABASE_PATH); 
     if (!fileDir.exists()) 
      fileDir.mkdirs(); 

     File tempFile = new File(LOCAL_DATABASE_PATH + DATABASE_NAME); 

     try { 
      tempFile.createNewFile(); // here I catch exception 

      InputStream is = SplashActivity.this.getAssets().open(
        DATABASE_NAME); 
      FileOutputStream os = new FileOutputStream(new File(
        LOCAL_DATABASE_PATH, DATABASE_NAME)); 

      int bufferLength = 0; 
      byte[] buffer = new byte[2048]; 

      while ((bufferLength = is.read(buffer)) > 0) { 
       os.write(buffer, 0, bufferLength); 
      } 

      Preferences.getInstance(SplashActivity.this). 
        set(Preferences.IS_DATABASE_COPYING_ON_DEVICE, true); 

      is.close(); 
      os.close(); 



     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

我收到以下錯誤

java.io.IOException: open failed: EACCES (Permission denied) 
08-28 09:32:27.977 32558-32558/com.DriverNotes.AndroidMobileClientTest D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:948) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at com.DriverNotes.AndroidMobileClientTest.SplashActivity$DataBaseLoadTask.doInBackground(SplashActivity.java:73) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.lang.Thread.run(Thread.java:856) 
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) 
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.Posix.open(Native Method) 
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:941) 
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ ... 7 more 
+0

您是否獲得了寫入文件的權限? – 2014-08-28 06:47:42

+0

是的,當然,我添加了所有權限 – Artem 2014-08-28 06:50:41

+0

顯示您的manifest.xml文件 – Nabin 2014-08-28 06:54:06

回答

1

在創建tempFile嘗試使用fileDir您已經創建得到肯定它的存在。

File tempFile = new File(fileDir, DATABASE_NAME); 

在創建一個新的文件,你應該檢查它是否已經通過調用其exits()方法存在。然後,而不是再次打開它,你應該繼續使用它。或者至少在再次開放之前關閉它。

if(!tempFile.exists()) 
    tempFile.createNewFile(); 

InputStream is = SplashActivity.this.getAssets().open(
       DATABASE_NAME); 
FileOutputStream os = new FileOutputStream(tempFile); 

創建您FileOutPutStream使用tempFile或接近tempFile得到確保您的文件沒有鎖定。

+0

我仍然會遇到同樣的異常 – Artem 2014-08-28 07:48:51

+0

那麼您的代碼是否會輸入tempFile.creaNewFile()行,表示文件不存在?你能調試你的代碼,並告訴我確切地拋出異常的位置嗎? – 2014-08-28 07:55:14

+0

調用createNewFile()時發生異常。通過Total Comander,我發現該文件不在該程序中 – Artem 2014-08-28 07:59:23