[java]代码库
import java.io.*;
import java.util.*;
/**
* 对象流
*
* 以对象数据为单位进行读写
*
*/
public class ObjectFileTest {
public static void main(String[] args) {
try {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 35000,
new Date(1989, 10, 1));
staff[1] = new Manager("Carl Cracker", 75000,
new Date(1987, 12, 15));
staff[2] = new Employee("Tony Tester", 38000, new Date(1990, 3, 15));
int i;
System.out.println("Before writeObject:");
for (i = 0; i < staff.length; i++)
staff[i].print();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("employee.dat"));
out.writeObject(staff);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"employee.dat"));
Employee[] newStaff = (Employee[]) in.readObject();
for (i = 0; i < newStaff.length; i++)
newStaff[i].raiseSalary(100);
System.out.println("After readObject:");
for (i = 0; i < newStaff.length; i++)
newStaff[i].print();
} catch (Exception e) {
System.out.print("Error: " + e);
System.exit(1);
}
}
}
class Employee implements Serializable {
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, Date d) {
name = n;
salary = s;
hireDay = d;
}
public Employee() {
}
public void print() {
System.out.println(name + " " + salary + " " + hireYear());
}
public void raiseSalary(double byPercent) {
salary *= 1 + byPercent / 100;
}
public int hireYear() {
return hireDay.getYear();
}
}
class Manager extends Employee {
private String secretaryName;
public Manager(String n, double s, Date d) {
super(n, s, d);
secretaryName = "";
}
public Manager() {
}
public void raiseSalary(double byPercent) {
Date today = new Date();
double bonus = 0.5 * (today.getYear() + 1900 - hireYear());
super.raiseSalary(byPercent + bonus);
}
public void setSecretaryName(String n) {
secretaryName = n;
}
public String getSecretaryName() {
return secretaryName;
}
}