Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter() not writing to disk unless file is very large  RSS feed

 
Aaron Parker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My PrintWriter will write (some) data to disk if the file written is very large but none if it is small. The StringBuffer writes to screen ok with a small file, but won't write all of a large file, unless PrintWriter is commented out. (Small test file 355b, large 839k).



Help?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is being buffered by the BufferedWriter, and probably by the OS, too. After the out.print(), you'll need to close the file, so everything is flushed to disk. Call out.close(), and everything should be fine.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:


The autoFlush parameter forces a physical write with every call to println()

Alternatively, you can selectively flush your buffers to disk by calling out.flush()
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:

The autoFlush parameter forces a physical write with every call to println()


Mike is right in what he says; but if you look at Aaron's code, you'll see that he's calling "print()", not "println()", so this actually won't matter.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike is right in what he says; but if you look at Aaron's code, you'll see that he's calling "print()", not "println()", so this actually won't matter.


That's true, he needs to call out.println(sb) instead of out.print(sb)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!