Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Serialization - flush() required after using writeObject()?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following program is from Kathy and Bert's SCJP 5 Exam Study Guide book (chap 6 - Serialization)




The program compiles an runs w/o any problem.
My question is whether the os.flush(); is required?

Please advice.

Tq
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In serialization, normally flush is not used. But its better to use flush because its a security measure that is taken to ensure that any data remaining on the stream is written on the file. close() does the same thing but it is not meant to check the streams properly.

flush() is basically used when we write to files. Before closing the file we flush all the data.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a good practice in using flush() method. Just remember that readers have no flush()
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you both. I understand better now.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that it is not necessary to call flush as the close operation will flush pending data anyways. The implementation of close in class ObjectOutputStream is implemented as follows:



 
Marshal
Posts: 68909
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DG, welcome to the Ranch

I hope the previous posters aren't waiting for an answer (‍), but I am sure you are correct. In most cases of output streams and writers, closing the object flushes the stream too. If you look at the documentation, in this case, you will find, unfortunately, that it doesn't say anything about flushing the stream.
 
Saloon Keeper
Posts: 21966
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's cheap insurance. I'm pretty sure that most close() methods do invoke flush(), but check the docs for the class in question - and its superclasses. And when in doubt, flush anyway. Double-flushing does no harm.

I once worked with someone who thought that it was OK not to close files because the garbage collector would close them. In actuality (Java 1.4), resources would leak and buffers wouldn't flush. So I've been prone to force the matter myself.

 
Alas, poor Yorick, he knew this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic