看不懂的奇怪的原因,文件 [英] Can't read a file for strange reason

查看:109
本文介绍了看不懂的奇怪的原因,文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这些方法来读取和写入文件:

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似乎没什么问题,但是当我运行code,我发现了以下错误:

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)

为什么系统只读?我应该做的,当我创建的文件,如果它不被存在file.createNewFile()的东西;

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.

推荐答案

您可能在Linux上得到这个错误。
该文件系统挂载为只读。
所以你不能写入。

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

这篇关于看不懂的奇怪的原因,文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆