Sheehan Alam
Sheehan Alam

Reputation: 60869

Unable to write to file in Android, Read-Only Filesystem

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?

Upvotes: 1

Views: 4634

Answers (2)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143144

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.

Upvotes: 2

Lycha
Lycha

Reputation: 10177

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/

Upvotes: 0

Related Questions