ObjectStreams

public class Employee  implements Serializable {
     FileOutputStream out = new FileOutputStream(String fileName); or  new FileOutputStream(File file);;
  1. convert the FileOutputStream to an ObjectOutputStream
   ObjectOutputStream objout = new ObjectOutputStream(out);
  1. write the objects to the stream using writeObject method
       objout.writeObject(object);
  1. when you are finished, close the stream
objout.close();
      objout.writeInt(productList.size());  // write out the number of objects first
      for(int i = 0; i <productList.size(); i++) // then write out each object
        objout.writeObject(productList.get(i));
                  Product obj;
                  int size = objin.readInt();
                  for(int i = 0; i < size; i++)  // read in objects one at a time
                  {
                     obj = (Product) objin.readObject();   
                    productList.add(obj);
                  }


    private void saveAsItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsItemActionPerformed
        // Add your handling code here:
     
     // allow the user to specify the file to write to
     //
     JFileChooser chooser = new JFileChooser();
      int result = chooser.showSaveDialog(this);
      if(result != JFileChooser.APPROVE_OPTION)
          return;
      File file = chooser.getSelectedFile();
      fileName = file.getPath();
    
     setTitle("ObjectStream Demo : "+fileName); // set the title
     //
    // create an output stream connection to the file
     try{
      FileOutputStream out = new FileOutputStream(fileName);
      ObjectOutputStream objout = new ObjectOutputStream(out);
     //
    // write out the number of objects in the vector, followed by the objects themselves
    //
      objout.writeInt(productList.size());  // write out the number of objects first
      for(int i = 0; i < productList.size(); i++) // then write out each object
        objout.writeObject(productList.get(i));
    
      objout.close();
     }
     catch (IOException e)
     {
       System.out.println(e.getMessage());       
     }               
    }



    private void openItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openItemActionPerformed
        // Add your handling code here:
  try{
      //
      // allow the user to select the file
      JFileChooser chooser = new JFileChooser();
      int result = chooser.showOpenDialog(this);
      if(result != JFileChooser.APPROVE_OPTION)
          return;
      File file = chooser.getSelectedFile();
      fileName = file.getPath();
     //
      // display the file name in the title
      setTitle("ObjectStream Demo : "+fileName); // set the title
     //
     // create an object stream connection to the file
     //
      FileInputStream in = new FileInputStream(fileName);
      ObjectInputStream objin = new ObjectInputStream(in);
   //
  // Empty out the vector to receive the items
   //
      productList.removeAllElements(); // empty the list
      currentItem = -1;

  // read in the number of objects first
      int size;
     size = objin.readInt();
// then read in each object
      for(int i = 0; i < size; i++)  // read in objects one at a time
      {
         Object obj = objin.readObject();  
         productList.add(obj);
      }
      objin.close();
      currentItem = size-1;
      displayProduct();
     }
     catch (IOException e)
     {
       System.out.println("IOException : "+e.getMessage());       
     }
     catch(ClassNotFoundException e)
     {
       System.out.println("ClassNotFoundException : "+e.getMessage());       
    }
             
    }//GEN-LAST:event_openItemActionPerformed