Source Code : A program that serializes and deserializes an Employee array
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
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);
staff[0].setBuddy(staff[1]);
staff[1].setBuddy(staff[0]);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
"staff.dat"));
out.writeObject(staff);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"staff.dat"));
Employee[] staff2 = (Employee[]) in.readObject();
in.close();
for (Employee e : staff2)
System.out.println(e);
}
}
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;
}
Thank with us