File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes PrintWriter does not write. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PrintWriter does not write. Why?" Watch "PrintWriter does not write. Why?" New topic

PrintWriter does not write. Why?

Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
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?

Always say the TRUTH only
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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(...):


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

[ August 23, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

When do you close the file ?

[My Blog]
All roads lead to JavaRanch
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
That's right! I completely forgot to close the files. Thank you very much. :-)
I agree. Here's the link:
subject: PrintWriter does not write. Why?
It's not a secret anymore!