EROS |只读文件系统错误

时间:2014-05-01 10:56:13

标签: java android file permissions

在我的Android应用程序中,我在txt文件中记录错误。但是当我尝试创建文件时,我遇到了错误Java.io.IOException: open failed: EROS( Read Only file system error)。我在AndroidManifest.xml中添加了写入权限,但没有区别。怎么解决?

代码

    try {       
          File file =new File("Log.txt");
            if(!file.exists()){
                file.createNewFile();
        } 
        catch (Exception e) 
        {
            Log.w("tun tun", e.toString());
        }

2 个答案:

答案 0 :(得分:1)

这样做

存储在外部存储设备(SDCARD)

try {
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Log.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            Log.w("tun tun", e.toString());
        }

存储在内部存储空间

try {
        File file = new File(context.getCacheDir() + File.separator + "Log.txt");

    if (!file.exists()) {
        file.createNewFile();
    }
} catch (Exception e) {
    Log.w("tun tun", e.toString());
}

答案 1 :(得分:1)

尝试以下代码: -

问题是你没有添加路径。

String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt";
File f = new File(filePath);

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

refrence

path = Environment.getExternalStorageDirectory();
File file = new File(path, "/" + fname);

Data directory has no read/write permission in Android