I am trying to write a file in Android.

private void writeScoreToFile(BlastScore result)
    {
        try{
            FileWriter fstream = new FileWriter(CaptureActivity.BLAST_SCORES,true);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(Integer.toString(result.getBlastScore()));
            out.close();
        }catch (Exception e){
            System.err.println("Write Error: " + e.getMessage());
        }
    }

I get the error:

10-28 21:04:11.200: W/System.err(669): Write Error: /BlastScores.txt (Read-only file system)

How can I resolve this?

有帮助吗?

解决方案

You're trying to write to the root directory. You probably want to be writing to your app's directory, not root.

To do that, use Context.openFileInput and Context.openFileOutput instead of new FileWriter.

其他提示

Based on the error it is trying to write a file to the filesystem root. Your application is only allowed write to specific folders.

Found this on an article:

/data/data/your_project_package_structure/files/
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top