简体   繁体   English

为什么我无法读取只读文件?

[英]Why I can't read a read only file?

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

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. 当然,您还必须将Context传递给函数。

You can only use File with a full path. 您只能使用具有完整路径的File For accessing your private files, use Context, just as you do when saving the file. 要访问私有文件,请使用Context,就像保存文件时一样。

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

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM