ID.java Source code

Java tutorial

Introduction

Here is the source code for ID.java

Source

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

class ID implements Serializable {
}

class Employee implements Serializable {
    private String name;

    private ID myID;

    Employee(String nm, ID h) {
        name = nm;
        myID = h;
    }

    public String toString() {
        return name + "[" + super.toString() + "], " + myID + "\n";
    }
}

public class MainClass {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ID id = new ID();
        List employees = new ArrayList();
        employees.add(new Employee("A", id));
        employees.add(new Employee("B", id));
        employees.add(new Employee("C", id));
        System.out.println("employees: " + employees);
        ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
        ObjectOutputStream o1 = new ObjectOutputStream(buf1);
        o1.writeObject(employees);
        o1.writeObject(employees);

        ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
        ObjectOutputStream o2 = new ObjectOutputStream(buf2);
        o2.writeObject(employees);

        ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
        ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
        List emp1 = (List) in1.readObject(), emp2 = (List) in1.readObject(), emp3 = (List) in2.readObject();
        System.out.println("emp1: " + emp1);
        System.out.println("emp2: " + emp2);
        System.out.println("emp3: " + emp3);
    }
}

/*
*/