Slim
Slim

Reputation: 1744

Can't write my arrayList to a file as expected

I have this method, supposed to write an arrayList to a file:

private ArrayList<String> readFromFile() {
    String ret = "";
    ArrayList<String> list = new ArrayList<String>();
try {
    InputStream inputStream = openFileInput("jokesBody.bjk");

    if (inputStream != null) {
        InputStreamReader inputStreamReader = new InputStreamReader(
                inputStream);
        BufferedReader bufferedReader = new BufferedReader(
                inputStreamReader);
        String receiveString = "";
        StringBuilder stringBuilder = new StringBuilder();

        while ((receiveString = bufferedReader.readLine()) != null) {
            list.add(receiveString);
        }

        inputStream.close();
        ret = stringBuilder.toString();
        System.out.println("DA CRAZY FILE: " + ret);
    }
} catch (FileNotFoundException e) {
    Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
    Log.e("login activity", "Can not read file: " + e.toString());
}

return list;
}

The problem with it is that it writes the values like [item1, item2, item3] and later when I need to load a the values back to a listArray it's loading the whole line at index 0. Now I have found the corerct way to write and read the arrayList, but I'm having troubles accessing teh file.

Here is the code I tried:

     private void writeToFile(ArrayList<String> list) {
                try {

                     FileOutputStream fos = new FileOutputStream("jokesBody.bjk");
                        ObjectOutputStream oos = new ObjectOutputStream(fos);   
                        oos.writeObject(list); // write MenuArray to ObjectOutputStream
                        oos.close(); 

                } catch (IOException e) {
                    Log.e("Exception", "File write failed: " + e.toString());

                        }
}

But it throws the following exception:

02-12 09:21:10.227: E/Exception(2445): File write failed: java.io.FileNotFoundException: /jokesBody.bjk: open failed: EROFS (Read-only file system)

Where is the mistake, where is the default app file location? I know that I'm missing something small, but as an android beginner, I'm not able to spot it.

Upvotes: 2

Views: 327

Answers (4)

Balder
Balder

Reputation: 8708

When you are developing for Android, you must get the OutputStream from the Context:

fos = context.openFileOutput("jokesBody.bjk", Context.MODE_PRIVATE);

An explanation about how to work with files on Android is here: Saving Files

Upvotes: 1

Rarw
Rarw

Reputation: 7663

I don't think you're actually saving the file where you think you are. Look at this tutorial on writing a file to external storage. A few things:

(1) You need to request permission in your manifest to write to external storage. If not you will end up with a read only situation.

(2) You need to get the external storage directory in your code before you write to it. This should be preceeded with a general check as to whether your file directory is writeable in the first place:

public boolean isExternalStorageWritable() {    
    String state = Environment.getExternalStorageState();    
    if (Environment.MEDIA_MOUNTED.equals(state)) {        
        return true;    
    }    
    return false;
}

You can then create a specific directory for the files you want to store and store them in that location so you can find them later. For example:

public File getAlbumStorageDir(String albumName) { 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);    
    if (!file.mkdirs()) {        
        Log.e(LOG_TAG, "Directory not created");    
    }    
    return file;
}

You can then write content to the file that is returned

Upvotes: 1

AnOldSoul
AnOldSoul

Reputation: 4197

Your file seems to be read only. You cannot write to a read only file!!!

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272247

Isn't this:

java.io.FileNotFoundException: /jokesBody.bjk: open failed: EROFS (Read-only file system)

the issue ? You're writing to a non-writeable area. Change where you're writing to (perhaps creating a temporary file would be a simple first step - I'm not familiar with Android but I assume this is possible)

Upvotes: 3

Related Questions