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

Does fileWriter.flush() is always necessary?  RSS feed

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Do I always have to call flush before to close the stream?
Like:
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.
don't mind. I was outdated.
http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!