java.io.FileNotFoundException:打开失败:EROFS(只读文件系统)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27103877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:13:51  来源:igfitidea点击:

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

javaandroidxmlandroid-studio

提问by Mark Cortejo

I have this code that generates an XML file and saves to an external directory.

我有这个生成 XML 文件并保存到外部目录的代码。

    try //database structure
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        //test report elements, does not reflect the real database in the future
        //mandatory/ username, location, caption, media, time(Actually, it's best if the server determines recieve time)
        //optional/
        //reportdata elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("reportdata");
        doc.appendChild(rootElement);

        //username elements
        Element username = doc.createElement("username");
        username.appendChild(doc.createTextNode("testusername"));
        rootElement.appendChild(username);

        //Location ELEMENTS
        //latitude elements
        Element lat = doc.createElement("latitude");
        lat.appendChild(doc.createTextNode(String.valueOf(latitude)));
        rootElement.appendChild(lat);
        //longitude
        Element longi = doc.createElement("longitude");
        longi.appendChild(doc.createTextNode(String.valueOf(longitude)));
        rootElement.appendChild(longi);

        //caption text elements

        Element capt = doc.createElement("caption");
        capt.appendChild(doc.createTextNode(captionText.getText().toString()));
        rootElement.appendChild(capt);

        //tag elements
        String[] tagArr = new String[selectItems.size()];
        tagArr = selectItems.toArray(tagArr);
        Element tags = doc.createElement("tags");
        rootElement.appendChild(tags);
        int o = selectItems.size();
        for(String s: tagArr) {
            Element tagname = doc.createElement("tagname");
            tagname.appendChild(doc.createTextNode(s));
            tags.appendChild(tagname);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        //StreamResult result = new StreamResult(System.out);
        StreamResult result = new StreamResult(new File(android.os.Environment.getRootDirectory(), "upload_data.xml"));
        transformer.transform(source, result);
        Log.d("MESSAGE", result.toString());

After the creation of the XML file, this exception was raised

创建 XML 文件后,引发此异常

D/XML TransformerException﹕ java.io.FileNotFoundException: /system/upload_data.xml: open failed: EROFS (Read-only file system)

Any way to fix this?

有任何解决这个问题的方法吗?

回答by ben75

... saves to an external directory

...保存到外部目录

This is not quite right. You are trying to save it in the rootsystem partition which is always read-only.

这不太对。您正在尝试将其保存在始终只读的系统分区中。

From javadoc of Environment.getRootDirectory():

来自Environment.getRootDirectory() 的javadoc :

Return root of the "system" partition holding the core Android OS. Always present and mounted read-only.

返回包含核心 Android 操作系统的“系统”分区的根目录。始终存在并以只读方式安装。

Solution : just save your file somewhere else :

解决方案:只需将您的文件保存在其他地方:

StreamResult result = new StreamResult(new File(android.os.Environment.getExternalStorageDirectory(), "upload_data.xml"));

Note that :

注意 :

  • you must ask the permission to write to the external storage :
  • 您必须请求写入外部存储的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • External storage is not always available. The partition may not be mounted -yet- when you try to write on it. (just be careful)
  • 外部存储并不总是可用。当您尝试在其上写入时,该分区可能尚未安装 - 还没有。(小心点)