This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Is my buffer messed up?  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the code I will paste below. In it I have various print statements that print to System.out and to a file. Printing to System.out always works, so I know that the code is doing what it's supposed to do but for some reason I can't always get it to print to the file. I put in duplicate print statements right next to one another, one printing to System.out and one printing to the file, with buffer flushes surrounding them. So I know that the buffer is not full. In the file it prints the list of averages as well as "sortedMap.size is 52" from the second method but, "Sorted Averages, Indices" is only ever printed to System.out. What the heck?
Thanks for any ideas, Rebecca


[ EJFH: Added "CODE" tags so code formatting shows. ]
[ November 11, 2004: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the line

out.println("\n Sorted Averages, Indices \n\n");

I see no out.flush() calls, so indeed, the data is being held up in a buffer. But perhaps more seriously, and to the point, you don't ever close "out". If you closed the file at the end, all the output would be flushed automatically. It's often nice call close() in a finally block, like



so that close() is called regardless of previous I/O errors.
 
Rebecca Witmer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks. That fixed it. I didn't realize that you had to flush a buffer to be certain you got it to print whatever was in it. I thought flushing was just sort of a way to get it unclogged if it was trying to hold too much stuff at once before writing the stuff to a file. You guys are a big help and allow me not to irritate my more knowledgable coworkers nearly as much. :-)
Rebecca
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rebecca Witmer:
I thought flushing was just sort of a way to get it unclogged if it was trying to hold too much stuff at once before writing the stuff to a file.

The buffer will flush() itself for vaious reasons, most notably when it gets full. You can call flush() yourself if you want whatever might still be buffered to be written immediately. Finally, closing the stream flushes the buffer.

To clarify then, you never need to call flush(), but you may. Usually you would do this if some other process is reading from the file/socket to which you're writing and you want to make sure they aren't waiting for your buffer to flush() itself.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in the case of you never need to call flush().

That stream is not buffered.

Guy
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!