Take the 2-minute tour ×

I am working on this project where the user enters some data which is written to an XML file.This part is working fine. Now when the user runs the program he should be able to append to that file. Instead it creates a new file with just one entry! A fileoutput stream is also not the solution.

Here is the code for serializing to XML

String medicine=medicfield.getText();
    String doctor=dnamefield.getText();
    int duration=Integer.parseInt(dodfield.getText());
    int amount=Integer.parseInt(cyclefield.getText());

    int inter=Integer.parseInt(intval.getText());
    PrescripManager pm=new PrescripManager();

        FileOutputStream file = new FileOutputStream("file.xml");

    JAXBContext jaxbContext = JAXBContext.newInstance(PrescripManager.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed

    jaxbMarshaller.marshal(pm, file);

    catch(Exception ex)

And the Class::

public class PrescripManager {
private String medname,docname;
private int interval,dcycle,dosage;
private Date dt;
public String getmedName() {
    return medname;

public void setmedName(String medname) {
    this.medname = medname;
public String getdocName() {
    return docname;

public void setdocName(String docname) {
    this.docname = docname;
public int getInterval() {
    return interval;

public void setInterval(int interval) {
    this.interval = interval;
public int getDcycle() {
    return dcycle;

public void setDcycle(int dcycle) {
    this.dcycle = dcycle;
 public int getDosage() {
    return dosage;

public void setDosage(int dosage) {
    this.dosage = dosage;

share|improve this question
You might want to add some more info: How does your code look that is currently trying to append something? –  Nihathrael Nov 13 '12 at 7:50
Have you tried using the FileOutputstream constructor with the boolean append as a parameter ? docs.oracle.com/javase/1.4.2/docs/api/java/io/…, boolean) –  LB40 Nov 13 '12 at 8:20
adding Boolean true destroys the structure of XML and all data is written in a straight line –  lazyprogrammer Nov 13 '12 at 8:32

3 Answers 3

You can use the "FileWriter" class which allows you to write at the end of a file. See


This link should help you:


I think it should work if you use

FilterWriter file = new FileWriter( "file.xml" , true );

instead of

FileOutputStream file = new FileOutputStream("file.xml");
share|improve this answer
not working ,destroys the XML Structure –  lazyprogrammer Nov 13 '12 at 8:35

First of all, you are writing an XML file. You can not just append to an XML file, because that would mean you are writing after then closing top level tag, resulting in invalid XML file.

You have at least three choices:

  • read old file in, add to the actual data, then write entire XML-file back.
  • write multiple files, each a valid XML file, with sequence number or timestamp in file name.
  • do not use XML, use a format which can be appended to

As a side note, if you want to append to file, you can open it in append mode. That will make every write to it append (at least on Unix, when file is opened in append mode, and I presume it works the same in Windows).

How to open file in append mode in Java: http://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File, boolean)

share|improve this answer
•do not use XML, use a format which can be appended to How to do this or **if you want to append to file, you can open it in append mode. That will make every write to it append ** this –  lazyprogrammer Nov 13 '12 at 8:31
Recommend any other format –  lazyprogrammer Nov 13 '12 at 8:33
recommend other format –  lazyprogrammer Nov 13 '12 at 8:42
@anirudhvarma Minimal effort solution using your existing code would be to write like your current code except in append mode, so result is invalid XML file with many valid pieces of XML in it. Then, when reading back, split the file into the valid XML "files", and feed these to unmarshaller one-by-one. You could use either custom inputstream, or read file and store pieces into many ByteArrayInputStreams. –  hyde Nov 13 '12 at 8:43

You are using JAXB to process XML files, so it's better that you change your XML file format and Java class to support this.

You can add a new class as collections of PrescripManager class instances. Something like PrescripManagerList.

public class PrescripManagerList{

  @XmlElementWrapper(name = "prescripManagers")
  @XmlElement(name = "prescripManager")
  private ArrayList<PrescripManager> prescripManagers;

When you running your code, try to read an existing XML file using JAXB unmarshaller to get a PrescripManagerList object, then add a new PrescripManager object to the ArrayList, then write the updated PrescripManagerList object to file using JAXB marshaller.

share|improve this answer

Your Answer


