0

I have this method supposed to read a file:

 /* 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;
    }

When I call it, it returns:

02-16 06:15:32.686: E/log activity(1380): Can not read file: java.io.IOException: open failed: EROFS (Read-only file system)

Even, if the file is read only, why I can't read it? I really can't understand what is wroong. I have this premission in my manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Can someone give me a clue? I know that I'm missing something small, but I really can't spot it.

Here is how I write the file:

/* Write content to a file */
    private void writeToFile(ArrayList<String> list, Context cont) {
        File file = new File("jokesBody1.bjk");     
        FileOutputStream fos;
        if(list != null){
        try {           
                fos = cont.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(); 
        }
        }
    }
chility
  • 746
  • 1
  • 9
  • 22
  • 1
    **Where** is your file? **File file = new File("jokesBody1.bjk");** I don't see any **path**. – Phantômaxx Feb 16 '14 at 11:21
  • Your `File` variable when writing the file is completely useless. – FD_ Feb 16 '14 at 11:31
  • possible duplicate of [Can't read a file for strange reason](http://stackoverflow.com/questions/21804263/cant-read-a-file-for-strange-reason) – Selvin Feb 16 '14 at 11:41

2 Answers2

4

You are trying to create the file, which of course fails on a read-only file.

Remove this line:

file.createNewFile();

This is usually used to create a new empty file before writing to it. You really don't need it if you just want to read a file that already exists.

EDIT:

Just use this:

ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesBody1.bjk"));

Of course, you'll also have to pass a Context to the function.

You can only use File with a full path. For accessing your private files, use Context, just as you do when saving the file.

Your full function should look like:

private ArrayList<String> readFromFile(Context context) {
    ArrayList<String> list = new ArrayList<String>();
    try {
        ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesBody1.bjk"));
        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;
}
FD_
  • 12,947
  • 4
  • 35
  • 62
  • `02-16 06:22:22.486: E/log activity(1450): Can not read file: java.io.FileNotFoundException: /jokesBody1.bjk: open failed: ENOENT (No such file or directory) ` this is what I got :( – chility Feb 16 '14 at 11:22
  • 2
    Where is your file located? – FD_ Feb 16 '14 at 11:23
1

You aren't specifying any path:

File file = new File("jokesBody1.bjk");

So, you are not saying the app WHERE to look for the file.

Maybe, you want to search it here?

Environment.getExternalStorageDirectory()
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115