0

我正在尝试导入一个存储在我的 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;
    }

这个错误是什么意思,我以前从未遇到过这个错误,如何正确导入数据库文件

4

1 回答 1

6

你的“to”通道应该是一个文件输出流,因为你想写入(而不是读取)这个文件:

FileChannel toChannel = new FileOutputStream(oldDB).getChannel();
于 2012-12-15T17:42:02.233 回答