java - Android 上的 JDom : error opening trace file: No such file or directory

标签 java android xml jdom

我不明白为什么会收到此错误,我正在尝试写入文件,而不是从中读取。起初我收到一个错误,因为我没有将 jar 放入 libs 文件夹中,现在我收到此错误。这是我的代码,我们将不胜感激! :)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    writeXML();


}




private void writeXML() {

    try{
    Document doc = new Document();

    Element theRoot = new Element("tvshows");
    doc.setRootElement(theRoot);

    Element show = new Element("show");
    Element name = new Element("show");
    name.setAttribute("show_id", "show_001");

    name.addContent(new Text("Life On mars"));

    Element network = new Element("network");
    network.setAttribute("country", "US");

    network.addContent(new Text("ABC"));

    show.addContent(name);
    show.addContent(network);

    theRoot.addContent(show);

    // - -



    Element show2 = new Element("show");
    Element name2 = new Element("show");
    name2.setAttribute("show_id", "show_002");

    name2.addContent(new Text("Life On mars"));

    Element network2 = new Element("network");
    network2.setAttribute("country", "US");

    network2.addContent(new Text("ABC"));

    show2.addContent(name2);
    show2.addContent(network2);

    theRoot.addContent(show2);

    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileOutputStream(new File("C:\\Users\\jmckay\\Desktop\\jdomMade.xml")));


    TextView tv = (TextView)findViewById(R.id.text);
    tv.setText("done");
    }catch(Exception e){
        e.printStackTrace();
    }
}

最佳答案

您似乎正在尝试保存到 Windows 文件地址,而您无法从 Android 设备(模拟设备或其他设备)执行此操作。您可以使用openFileOutput获取 FileOutputStream 写入内部存储中的文件(特定于您的应用程序):

xmlOutput.output(doc, openFileOutput(yourFilename, Context.MODE_PRIVATE));

查看 Storage Options 上的开发人员指南,特别是内部和外部存储。

关于java - Android 上的 JDom : error opening trace file: No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18044029/

相关文章:

String.replace 上的 java.lang.OutOfMemoryError

java - usernametoken-auth rampart/axis2 1.6.2

android - Android CameraX 预览质量不佳(取景器)

xml - 如何通过 Ruby 中的 RELAX NG 验证 XML?

java - org.xml.sax.SAXParseException; lineNumber : 6; columnNumber: 122; cvc-elt. 1:找不到元素 'beans' 的声明

java - 如何将我自己的 .class 文件导入到 RingoJS 应用程序中?

java - Alert Builder 我错过了什么

android - Android 的 FFMPEG 格式设置

Android(Linux) 的 mount(8) 通过本地 shell 和 adb 影响不同的进程

c - 如何从 xml schema(xsd) 的复杂类型获取正则表达式?