A program that serializes and deserializes an Employee array : ObjectOutputStream « File Input Output « Java

A program that serializes and deserializes an Employee array

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

 * A program that serializes and deserializes an Employee array.
public class SerializeEmployeeTester {
  public static void main(String[] args) throws IOException,
      ClassNotFoundException {
    Employee[] staff = new Employee[2];
    staff[0] = new Employee("Fred Flintstone", 50000);
    staff[1] = new Employee("Barney Rubble", 60000);
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(
    Employee[] staff2 = (Employee[]) in.readObject();
    for (Employee e : staff2)

class Employee implements Serializable {
  public Employee(String name, double salary) {
    this.name = name;
    this.salary = salary;
    this.buddy = this;

  public void setBuddy(Employee buddy) {
    this.buddy = buddy;

  public String toString() {
    return getClass().getName() + "[name=" + name + ",salary=" + salary
        + ",buddy=" + buddy.name + "]";

  private String name;
  private double salary;
  private Employee buddy;


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.implements Externalizable
5.Writing objects to file with ObjectOutputStream
6.Serializing an Object (JButton)
7.Override writeObject(ObjectOutputStream oos) and readObject(ObjectInputStream ois)
8.Returns a byte array from the given object.