我这几个程序,有两个能写,但回读时就找不到文件,而另外两个却没有这个问题,小弟百思不得其解,请大哥大姐指导一下。

解决方案 »

  1.   

    import java.io.*;
     import java.util.*;
    public class DataFileTest
     {
        
        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,1990,3,15);
           
           try
           {
            PrintWriter out=new PrintWriter(new FileWriter("employee.dat"));
            writeData(staff,out);
            out.close();
           
            BufferedReader in=new BufferedReader(new FileReader("employee.dat"));
            Employee[] newStaff=readData(in);
            in.close();
            for(int i=0;i<newStaff.length;i++)
            System.out.println(newStaff[i]);
           }
           catch(IOException exception)
           {
            exception.printStackTrace();
           }
        }
        static void writeData(Employee[] e,PrintWriter out) throws IOException
        {
         out.println(e.length);
        
         for(int i=0;i<e.length;i++)
         e[i].writeData(out); 
        }
        static Employee[] readData(BufferedReader in)throws IOException
        {
         int n=Integer.parseInt(in.readLine());
        
         Employee[] e=new Employee[n];
         for(int i=0;i<n;i++)
         {
         e[i]=new Employee();
         e[i].readData(in);
         }
         return e;
        }
    }
    class Employee
    {
    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="+name+".salary="+salary+".hireDay="+hireDay+"]";
    }
    public void writeData(PrintWriter out)throws IOException
    {
    GregorianCalendar calendar=new GregorianCalendar();
    calendar.setTime(hireDay);
    out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+calendar.get(Calendar.DAY_OF_MONTH));
    }
    public void readData(BufferedReader in) throws IOException
    {
    String s=in.readLine();
    StringTokenizer t=new StringTokenizer(s,"|");
    name=t.nextToken();
    salary=Double.parseDouble(t.nextToken());
    int y=Integer.parseInt(t.nextToken());
    int m=Integer.parseInt(t.nextToken());
    int d=Integer.parseInt(t.nextToken());
    GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
    hireDay=calendar.getTime();
    }
    private String name;
    private double salary;
    private Date hireDay;
    }
      

  2.   

    这是第二个
    import java.io.*;
     import java.util.*;
     class Employee implements Serializable
     {
      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="+name+",salary="+salary+",hireDay="+hireDay+"]";
      }
      private String name;
      private double salary;
      private Date hireDay;
     }
     class Manager extends Employee
     {
      public Manager(String n,double s,int year,int month,int day)
      {
      super(n,s,year,month,day);
      bonus=0;
      }
      public double getSalary()
      {
      double baseSalary=super.getSalary();
      return baseSalary+bonus;
      }
      public void setBonus(double b)
      {
      bonus=b;
      }
      public String toString()
      {
      return super.toString()+"[bonus="+bonus+"]";
      }
      private double bonus;
     }
    public class ObjectFileTest
     {
        
        public static void main(String[] args) 
        {
           Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
           boss.setBonus(5000);
           
           Employee[] staff=new Employee[3];
           
           staff[0]=boss;
           staff[1]=new Employee("Harry Hacker",50000,1989,10,1);
           staff[2]=new Employee("Tony Tester",40000,1990,3,15);
           
           try
           {
            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();
            in.close();
           
            for(int i=0;i<newStaff.length;i++)
            System.out.println(newStaff[i]);
           }
           catch(Exception e)
           {
            e.printStackTrace();
           }
        }
    }
      

  3.   


    import java.io.*;
     import java.util.*;
     class Employee implements Serializable
     {
      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="+name+",salary="+salary+",hireDay="+hireDay+"]";
      }
      private String name;
      private double salary;
      private Date hireDay;
     }
     class Manager extends Employee
     {
      public Manager(String n,double s,int year,int month,int day)
      {
      super(n,s,year,month,day);
      bonus=0;
      secretary=null;
      }
      public void setSecretary(Employee s)
      {
      secretary=s;
      }
      public double getSalary()
      {
      double baseSalary=super.getSalary();
      return baseSalary+bonus;
      }
      public void setBonus(double b)
      {
      bonus=b;
      }
      public String toString()
      {
      return super.toString()+"[bonus="+bonus+"]"+"[secretary="+secretary+"]";
      }
      private double bonus;
      private Employee secretary;
     }
    public class ObjectRefTest 
    {
        
        public static void main(String[] args) 
        {
            Employee harry=new Employee("Carl Cracker",50000,1989,10,1);
            Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
            boss.setSecretary(harry);
            
            Employee[] staff=new Employee[3];
            staff[0]=boss;
            staff[1]=harry;
            staff[2]=new Employee("Tony Tester",40000,1990,1,15);
            try
            {
             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();
                in.close();
                
                newStaff[1].raiseSalary(10);
                
                for(int i=0;i<newStaff.length;i++)
                System.out.println(newStaff[i]);
            }
            catch (Exception e)
            {
             e.printStackTrace();
            }
        }
    }