This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to a file

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a java program to make a simple XML files for my Deployment descriptors in Tomcat, in windows 7.
The problems are :
1.The web.txt file is blank
2.The web.txt file is not renamed to web.xml

I have a doubt, can i make a .xml file and write text to it OR, do i have to make a .txt file - write the text - and then rename it to .xml ?

The code :



 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
1.The web.txt file is blank

Try flushing the printWriter:



Rahul Sudip Bose wrote:
I have a doubt, can i make a .xml file and write text to it OR, do i have to make a .txt file - write the text - and then rename it to .xml ?

Yes, You can create an xml file and write to it.

And one more thing, don't forget to close the PrintWriter
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishwanath is right. You don't flush() or close() the stream, so it may still cache the data. flush() ensures this cached data is written to the file; close() automatically flushes as well.

As for the renaming not working, that's because you didn't close() the stream. As a result, the JVM still has a lock on the file, and that causes the renaming to fail.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for making it work.

Theory for other readers with same doubt : flush and close
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic