There are a couple of options here.
I assume your result is a StreamResult , which you create with a String specifying the path to the target file. You can consider opening FileOutputStream yourself and create a StreamResult with this. Then, when the transformer is complete, add a line terminator, flush and close the stream. In the absence of restrictions, otherwise I would use this approach.
If you want to reopen the file, as shown in the question, you will need to use the FileOutputStream constructor, which accepts an optional append . Set this value to true to avoid smoothing the result of the conversion just completed.
You can also study setting the indent output property in your transform, or by including the desired line terminator directly in your template.
source share