Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter does not write. Why?

 
Prosenjit Banerjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code writes 0 bytes to the destination file (the file gets created but has 0 byte). But the source file is reading quite well and prints on the screen. Can anybody please tell me where am I going wrong?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter is buffered. It flushes its buffer when you print a newline character, or when you call flush() on it. Add the following line after your line with out.print(...):

out.flush();

Note, you are not properly closing the input and output streams. Add the following lines at the end of your method:

out.close();
in.close();
[ August 23, 2007: Message edited by: Jesper Young ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do you close the file ?
 
Prosenjit Banerjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right! I completely forgot to close the files. Thank you very much. :-)
 
Die Fledermaus does not fear such a tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!