1

I have these methods for reading and writing to a file:

/* Write content to a file */
    private void writeToFile(ArrayList<String> list) {
        File file = new File("jokesBody1.bjk");     
        FileOutputStream fos;
        if(list != null){
        try {           
                file.createNewFile();
                fos = openFileOutput("jokesBody1.bjk",Context.MODE_PRIVATE);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject(list);
                out.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace(); 
        }
        }else{
            try {
                file.createNewFile();
                fos = openFileOutput("jokesBody1.bjk",Context.MODE_PRIVATE);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject("");
                out.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace(); 
        }
        }
    }

    /* Read file's content */
    private ArrayList<String> readFromFile() {
        File file = new File("jokesBody1.bjk");
        ArrayList<String> list = new ArrayList<String>();
        try {
            file.createNewFile();
            ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
            try {
                list = (ArrayList)ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            ois.close();
            } catch (IOException e) {
            Log.e("log activity", "Can not read file: " + e.toString());
        }

        return list;
    }

Everyrhing seems to be fine to me, but when I run the code I'm getting the following error:

02-15 17:02:07.655: E/log activity(1882): Can not read file: java.io.IOException: open failed: EROFS (Read-only file system)

Why the system is read only? Should I do something when I'm creating the file if it does not exist by file.createNewFile();?

I know that I'm missing something extremely small, but as a total beginner, I'm not able to spot it.

chility
  • 746
  • 1
  • 9
  • 22
  • You don't need to call createNewFile () when you're about to open a file for output, and you *shouldn't* call it when you're about to open it for input, otherwise you get an empty file. – user207421 Feb 15 '14 at 23:18

1 Answers1

1

You get this error probably on Linux.
This file system is mounted as read-only.
So you cannot write to it.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Check the permissions, the type of the file system on which this file resides, anything in that direction. This is basically what this error means. Could be that the account you're using has no permissions to write to it or something similar. – peter.petrov Feb 15 '14 at 22:09
  • I don't know where the emulator is creating the application dirs. :( – chility Feb 15 '14 at 22:11
  • See also: http://stackoverflow.com/questions/15711098/trying-to-create-a-file-in-android-open-failed-erofs-read-only-file-system – peter.petrov Feb 15 '14 at 22:11
  • 1
    "I don't know where the emulator is creating the application dirs." I guess now is a good time to learn these details :) – peter.petrov Feb 15 '14 at 22:12
  • See also: http://stackoverflow.com/questions/18418930/android-file-open-failed-erofs – peter.petrov Feb 15 '14 at 22:13
  • If you Google search your error you'll find lots of resources. – peter.petrov Feb 15 '14 at 22:13