jQuery in Action, 3rd edition
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
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: 15082

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
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: 42965
Does the delete() method return true?
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
I agree. Here's the link: http://aspose.com/file-tools
subject: problem with writing the file
It's not a secret anymore!