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 に答える