title: When to use flush() in java?
body: <pre><code>import java.io. * ;
  public class Ser {
  
      public static void main(String args[]) {
  
          try {
              John myObj = new John("Sachin", "Cricket");
              System.out.println(myObj);
              FileOutputStream fos = new FileOutputStream("FileName");
              ObjectOutputStream oos = new ObjectOutputStream(fos);
              oos.writeObject(myObj);
              oos.flush();
              oos.close();
          } catch (Exception e) {
              System.out.println("Expection" + e);
              System.exit(0);
          }
  
          try {
              John myObj2;
              FileInputStream fis = new FileInputStream("FileName");
              ObjectInputStream ois = new ObjectInputStream(fis);
              myObj2 = (John) ois.readObject();
              ois.close();
              System.out.println("New Object" + myObj2);
          } catch (Exception e) {
              System.out.println("Expection" + e);
              System.exit(0);
          }
  
      }
  }
  
  class John implements Serializable {
  
      private String name;
      private String department;
  
      public John(String name, String department) {
          this.name = name;
          this.department = department;
      }
  
      public String toString() {
          return "Name" + name + " " + "Department" + department;
  
      }
  
  }
  </code></pre>
  
  <p>I have a few questions in the above example. </p>
  
  <ol>
  <li>When to use flush method and why do we use it?</li>
  <li>What does close method carry a score here?</li>
  <li><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.</li>
  <li>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.</li>
  </ol>