import java.io.*;
import java.util.*;
public class ObjectEmployeeDemo{
public static void main(String[] args){
Employee[] staff = new Employee[3];
staff[0] = new Employee("carl cracker",75000,1987,12,15);
staff[1] = new Employee("Harry Hacker",50000,1989,10,1);
staff[2] = new Employee("Tony tester",40000,1900,3,15);
try{
ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("employee.dat"));
for(int i=0;i<staff.length;i++)
obs.writeObject(staff[i]);
obs.close();
ObjectInputStream obin = new ObjectInputStream(new FileInputStream("emplayee.dat"));
Employee[] newStaff = (Employee[])obin.readObject();
obin.close();
for(int i=0;i<newStaff.length;i++)
System.out.println(newStaff[i]);
}catch(ClassNotFoundException e){e.printStackTrace();}
catch(IOException e)
{

e.printStackTrace();
}

}
}
class Employee implements Serializable{  //这里什么出错了?????? private String name;
private double salary;
private Date hireDay;
public Employee(){}
public Employee(String n, double s, int year,int month,int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1,day); hireDay = calendar.getTime();
}
public String getName(){return name;}
public double getSalary(){return salary;}
public Date getHireDay(){return hireDay;}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent /100;
salary += raise;
}
public String toString(){
return 
getClass().getName()+
"[name="+this.getName()+
",salary="+this.getSalary()+
",hireDay="+this.getHireDay()+
"]";
}}

解决方案 »

  1.   

    它只是个警告,让你加个序列化ID,IDE可以自动给你加!
      

  2.   


    private static final long serialVersionUID = 1L;//可以了
      

  3.   

    编译后还是不行,提示Employee有错误
      

  4.   

    对阿。你把命令行的所有东西都贴出来,这样便于分析阿,不过你的这个程序除了警告应该不会有错误的,除非你系统中没有那个employee.dat文件
    顺便问一下7楼,你的头像真漂亮。哈哈。是你本人吗
      

  5.   

    the type Employee is already define!!      //这个是在eclipse中出现的
    用记事本从新写,重新编译后为找不到employee.dat  //默认文件夹存在这个文件
      

  6.   

    ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("employee.dat"));
    ObjectInputStream obin = new ObjectInputStream(new FileInputStream("emplayee.dat"));     //注意看文件名不相同
      

  7.   


    import java.io.*; 
    import java.util.*; 
    public class ObjectEmployeeDemo{ 
    public static void main(String[] args){ 
    Employee[] staff = new Employee[3]; 
    staff[0] = new Employee("carl cracker",75000,1987,12,15); 
    staff[1] = new Employee("Harry Hacker",50000,1989,10,1); 
    staff[2] = new Employee("Tony tester",40000,1900,3,15); 
    try{ 
    ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("f:\\employee.dat")); 
    for(int i=0;i <staff.length;i++) 
    obs.writeObject(staff[i]); 
    obs.close(); 
    ObjectInputStream obin = new ObjectInputStream(new FileInputStream("f:\\employee.dat")); //这里的文件名改成了相同的//Employee[] newStaff = (Employee[])obin.readObject();     //编译没问题,运行就出问题,所以没用
    for(int i=0;i < 3;i++)                                      //你原来的数组长度改成了3
    System.out.println((Employee)obin.readObject());           //这里直接序列化解体
    obin.close();                                               //所以关闭流改到了这里
    }
    catch(ClassNotFoundException e){
    e.printStackTrace();

    catch(IOException e) 
    { e.printStackTrace(); 
    } } 

    class Employee implements Serializable{  private String name; 
    private double salary; 
    private Date hireDay; 
    public Employee(){} 
    public Employee(String n, double s, int year,int month,int day) 

    name = n; 
    salary = s; 
    GregorianCalendar calendar = new GregorianCalendar(year,month-1,day); hireDay = calendar.getTime(); 

    public String getName(){return name;} 
    public double getSalary(){return salary;} 
    public Date getHireDay(){return hireDay;} 
    public void raiseSalary(double byPercent) 

    double raise = salary * byPercent /100; 
    salary += raise; 

    public String toString(){ 
    return  
    getClass().getName()+ 
    "[name="+this.getName()+ 
    ",salary="+this.getSalary()+ 
    ",hireDay="+this.getHireDay()+ 
    "]"; 
    } } 其他应该没问题了