This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes problem with writing the file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with writing the file" Watch "problem with writing the file" New topic

problem with writing the file

trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 58
i havv to write to the file that deletes the old contents of the file and writes the new data.but the writing is performed by appending to old data in the file

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(rootNode);
FileOutputStream fos= new FileOutputStream("D:/jspp/web.xml");
StreamResult result = new StreamResult(fos);
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

You could try deleting the file before you open it for writing:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 58
i have triedlike this also but no use it is appending to old data.that file is not deleting
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41062
Does the delete() method return true?

Ping & DNS - my free Android networking tools app
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Ah, where exactly did this "rootNode" come from? Is it possible that this node actually has all the old data still attached to it? (Plus whatever new data you've added?) That would be my guess at this point. The way you're creating the FileOutputStream, it shouldn't matter whether you delete the old file or not - you're overwriting it. If you're seeing the old data appear again, it's probably because you're actually writing it again. Which could be because it's still attached to the rootNode.

"I'm not back." - Bill Harding, Twister
It is sorta covered in the JavaRanch Style Guide.
subject: problem with writing the file
Similar Threads
Writing XML File via Java Program
Format xml file output using DOM
File Output stream adding extra character while writing
XML on the fly using servlets
org.w3c.dom.* saves my XML with attributes in alphabetical order