2012-12-15 73 views
0

我想導入一個數據庫,該數據庫存儲在我的SD卡上,如果我想恢復某些內容,但是當我嘗試進口我得到NonWritableChannelException當從SD卡導入數據庫文件時出現NonWritableChannelException

錯誤

12-15 12:27:48.190: W/System.err(13599): java.nio.channels.NonWritableChannelException 
12-15 12:27:48.190: W/System.err(13599): at java.nio.FileChannelImpl.checkWritable(FileChannelImpl.java:85) 
12-15 12:27:48.190: W/System.err(13599): at java.nio.FileChannelImpl.transferTo(FileChannelImpl.java:399) 
12-15 12:27:48.190: W/System.err(13599): at com.tyczj.bowling.Bowlers$ImportData.importGames(Bowlers.java:944) 
12-15 12:27:48.200: W/System.err(13599): at com.tyczj.bowling.Bowlers$ImportData.doInBackground(Bowlers.java:914) 
12-15 12:27:48.200: W/System.err(13599): at com.tyczj.bowling.Bowlers$ImportData.doInBackground(Bowlers.java:1) 
12-15 12:27:48.200: W/System.err(13599): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
12-15 12:27:48.200: W/System.err(13599): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
12-15 12:27:48.210: W/System.err(13599): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
12-15 12:27:48.210: W/System.err(13599): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
12-15 12:27:48.210: W/System.err(13599): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
12-15 12:27:48.210: W/System.err(13599): at java.lang.Thread.run(Thread.java:856) 

這裏是我用來導入

public boolean importGames(){ 
     File newDB = new File(Environment.getExternalStorageDirectory() + "/BCAData/Games"); 
     File oldDB = new File(Environment.getDataDirectory()+"/data/my.app.package/databases/Games"); 
     if(newDB.exists()){ 
      try { 
       FileChannel fromChannel = new FileInputStream(newDB).getChannel(); 
       FileChannel toChannel = new FileInputStream(oldDB).getChannel(); 
       fromChannel.transferTo(0,fromChannel.size(),toChannel); //fails here 
       try{ 
        if(fromChannel != null){ 
         fromChannel.close(); 
        } 
       }finally{ 
        if(toChannel != null){ 
         toChannel.close(); 
        } 
       } 
       return true; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
     return false; 
    } 

方法這個錯誤是什麼MEA n,我以前從來沒有這樣做過,如何正確導入數據庫文件

回答

6

您的「to」通道應該是文件輸出流,因爲您想寫入(不讀取)此文件:

FileChannel toChannel = new FileOutputStream(oldDB).getChannel(); 
+0

這是它的感謝! – tyczj

+0

沒有麻煩!很高興我能幫上忙。 – Sam

+0

@薩姆謝謝,它的工作 –