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

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: 13875
    
  10

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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: http://aspose.com/file-tools
 
subject: PrintWriter does not write. Why?
 
Similar Threads
Code generation help
Returning A List Of Variables From A Folder Of Documents And Returning Them Into A New Document
Why notify is throwing an exception here ?
If String Contains (symbols) then call main method
Substring capture To Write A list Of instances in A New Document