簡體   English   中英

Android上的JDom:打開跟蹤文件時出錯:沒有這樣的文件或目錄

[英]JDom on Android: error opening trace file: No such file or directory

我不明白為什么會收到此錯誤,我試圖寫入文件,而不是讀取文件。 剛開始我遇到一個錯誤,因為我沒有在libs文件夾中放一個jar,現在我收到了這個錯誤。 這是我的代碼,將不勝感激! :)

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

請參閱有關存儲選項 (特別是內部和外部存儲)的開發人員指南。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM