This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 28822
212
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.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic