Serializing an Object (JButton) : ObjectOutputStream « File Input Output « Java






Serializing an Object (JButton)

  


import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);
    out.close();

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
  }
}

   
    
  








Related examples in the same category

1.new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(StringFileName)));
2.Create ObjectOutputStream out of FileOutputStream
3.Write different data types with ObjectOutputStream
4.A program that serializes and deserializes an Employee array
5.implements Externalizable
6.Writing objects to file with ObjectOutputStream
7.Override writeObject(ObjectOutputStream oos) and readObject(ObjectInputStream ois)
8.Returns a byte array from the given object.