0

I'm trying to write some XML lines in a XML file. However, I can only do it if the file doesn't exist, because if it exists I'll overwrite the information.

Here's my code:

public void writeToXMLSimulation() throws IOException {
    File f = new File("Simulation_" + pt.ipp.isep.gecad.masgrip.MASGriP_GUI.getContainerNameTxt().getText() + "_Details.xml");
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    XMLWriter xmlWriter = null;
    if (!f.exists()) {

        try {
            xmlWriter = new XMLWriter(new OutputStreamWriter(
                    new FileOutputStream(f), "UTF8"),
                    format);
            xmlWriter.write(configs.XMLwriterDOM4J.createXMLDocumentForSimulations(jLabelAL, jTextFieldAL, id));
        } finally {
            if (xmlWriter != null) {
                xmlWriter.flush();
                xmlWriter.close();
            }
        }

    } else {

        try {
            //I NEED SOMETHING HERE TO GET ME TO THE LAST LINE OF FILE

    xmlWriter.write(
configs.XMLwriterDOM4J.createXMLDocumentForSimulations(
jLabelAL, jTextFieldAL, id));
            } finally {
                if (xmlWriter != null) {
                    xmlWriter.flush();
                    xmlWriter.close();
                }
            }

        }

    }

What can I do, to make my code write after the last line? (without overwritting)

Thanks

ageoff
  • 2,798
  • 2
  • 24
  • 39
SaintLike
  • 9,119
  • 11
  • 39
  • 69
  • Rewrite the file, then add the new stuff at the end. – ageoff Jun 03 '13 at 16:31
  • You cannot append anything meaningful to an XML file. If it is to be a correctly structured file, all content needs to be inside a single, top-level tag. What are you really trying to accomplish? Can you give a short example of how an existing XML file should be changed? – Ted Hopp Jun 03 '13 at 16:35
  • I don't need it to be an XML file exactly. I just used the XML extention as an example. Because, later on, the files will be named: something.notxml I just want to know how to write after the last line of the file – SaintLike Jun 03 '13 at 16:39
  • @LiverpoolFTW how can I do that? You know of some example I can look at? – SaintLike Jun 03 '13 at 16:43
  • 1
    @EduardoRocha If all you want to do is add something to the end of your files, then look at the answer Aurand gave, or look at http://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java?rq=1. Rewriting the file would be useful only if you want to add stuff other then at the end. – ageoff Jun 03 '13 at 16:47

1 Answers1

1

In the general case, you can write to a file in append mode using:

new FileOutputStream(file, true);

The second parameter is a boolean with true meaning append and false meaning overwrite. The default is false (as you have no doubt discovered).

That being said, appending data to the end of an XML file probably does not make sense. It would be much better to use an XML reader to read the XML file into a Document object, add elements to that object, and then write that object back to disk.

Aurand
  • 5,487
  • 1
  • 25
  • 35