我有这个方法,应该将arrayList写入文件:

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

它的问题在于,它会写入类似于[item1, item2, item3]的值,然后当我需要将值加载回alistArray时,它会加载索引0处的整行。现在我已经找到了corerct方法来编写和读取arrayList,但是我在访问这个文件时遇到了问题。
这是我试过的代码:
     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());

                        }
}

但它抛出了以下异常:
02-12 09:21:10.227: E/Exception(2445): File write failed: java.io.FileNotFoundException: /jokesBody.bjk: open failed: EROFS (Read-only file system)

错误在哪里,默认的应用程序文件位置在哪里?我知道我漏掉了一些小东西,但作为一个android初学者,我无法发现它。

最佳答案

这不是吗?

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

这个问题?你在写一个不可写的区域。更改您的写作目的地(可能creating a temporary file would be a simple first step-我不熟悉android,但我认为这是可能的)

关于java - 不能按预期将我的arrayList写入文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21730776/

10-10 23:30
查看更多