• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Does fileWriter.flush() is always necessary?

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Do I always have to call flush before to close the stream?
Like:
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note close calls flush, so calling both is redundant. I would always call close. I'm not sure what happens in your code if an exception is thrown. It may make sense to call close in a finally clause.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html#close()
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

steve souza wrote:Note close calls flush, so calling both is redundant. I would always call close. I'm not sure what happens in your code if an exception is thrown. It may make sense to call close in a finally clause.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html#close()


Are you sure that close call flush? I'm reading a book that calls flush before close, and it would be redundant as you said.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why the link below isn't going directly to the close method, but if you look at the documentation for the close method you will see it calls flush.
[url]
http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html#close()[/url]
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

steve souza wrote: I'm not sure why the link below isn't going directly to the close method, but if you look at the documentation for the close method you will see it calls flush.
[url]
http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html#close()[/url]


The method fw.close calls the method close from OutputStreamWriter class. Although OutputStreamWriter extends the Writer class, it overrides the close method, and its method don't call flush.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry.
don't mind. I was outdated.
http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html
 
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you absolutely, positively HAVE to ensure it got written, call flush(). You can call flush anytime and you can call it as often as you like. If there's nothing to flush, the overhead is minimal.
 
Wanna see my flashlight? How about this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic