File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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");
fos.flush();
StreamResult result = new StreamResult(fos);
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14102
    
  16

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
Marshal

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


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

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