This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara 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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 60
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: 14116

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: 60
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: 41621
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
wood burning stoves
subject: problem with writing the file