• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is my buffer messed up?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that in the case of you never need to call flush().

That stream is not buffered.

Guy
 
reply
    Bookmark Topic Watch Topic
  • New Topic