• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Writing to a file

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :



 
Ranch Hand
Posts: 331
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22207
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for making it work.

Theory for other readers with same doubt : flush and close
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic