Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>have a few questions in the above example.</p> <ol> <li><p><em>When to use flush method and why do we use it?</em></p> <p>You usually won't need to <code>flush()</code> and <code>OutputStream</code>, it won't lose any data if you properly <code>close()</code> it in the end. Sometimes, you want to communicate to the underlying data sink (e.g. network <code>Socket</code>, local <code>File</code>, or another <code>OutputStream</code>) that you want any data written to the stream to be flushed/processed -- but as per the <a href="http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html#flush%28%29" rel="nofollow"><code>flush()</code></a> API doc, that's <strong>not guaranteed</strong> to happen.</p></li> <li><p><em>What does close method carry a score here?</em></p> <p>I really don't understand this part, but the <code>close()</code> closes an <code>OutputStream</code> and causes it to write out any data it still has buffered -- so there's no need to call <code>flush()</code> before <code>close()</code>. An <code>OutputStream</code> can not be written to after it has been closed.</p></li> <li><p><em><code>myObj2 = (John) ois.readObject();</code> ... please correct me if i am wrong, i am reading the file object and storing into another object and typecasting the file object.</em></p> <p>Wrong, you're reading from the <code>ObjectInputStream</code> <code>ois</code>, it's irrelevant where it has its data from (in your case it does, indeed, come from a file, but there's no "file object" in your code). </p> <p><code>ObjectInputStream</code> can reconstruct any data previously written/serialized by <code>ObjectOutputStream</code> -- so in this case you <strong>de-serialize</strong> a previously serialized object of type <code>John</code>. Because serialization only works on some Java types (primitives and implementors of <code>Serializable</code>), it doesn't know about your specific class <code>John</code>. But you know, you previously serizalized a <code>John</code> object, so you can safely cast from <code>Object</code> to <code>John</code> and assign it to a local variable.</p></li> <li><p><em>What are the alternatives of Serialization or persisting the data in Java. I don't want the data to get into file as byte stream.</em></p> <p>You could look into other serialization/marshalling approaches like <a href="http://xstream.codehaus.org/" rel="nofollow">XStream</a>, <a href="http://www.json.org/java/index.html" rel="nofollow">JSON</a> or roll your own (complex, only do this if you have a good reason). Most of these will prove more complex than the built-in serialization, so be sure you have a good reason to not just write "itno file as byte stream".</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload