XML file is not updating using the jdom

后端 未结 1 1058
慢半拍i
慢半拍i 2021-01-14 15:48

following is my java code for reading a xml file and updating some values in it.

 public static void writeLexicon(String word, String tag) {
    int newFreq         


        
相关标签:
1条回答
  • 2021-01-14 16:22

    This is a relatively common problem with many applications, not just JDOM.

    When you create a FileOutputStream, and write to it, you HAVE TO FLUSH IT AND CLOSE IT before exiting your program.

    Change:

    xmlOutput.output(doc, new FileOutputStream(new File("./src/Lexicon.xml")));
    

    to be (using try-with-resources):

    try (OutputStream fileout = new FileOutputStream(new File("./src/Lexicon.xml"))) {
        xmlOutput.output(doc, fileout);
    }
    

    or:

    OutputStream fileout = new FileOutputStream(new File("./src/Lexicon.xml"));
    xmlOutput.output(doc, fileout);
    fileout.flush();
    fileout.close();
    
    0 讨论(0)
提交回复
热议问题