日期:2014-05-20  浏览次数:20732 次

文件读取错误。请各位帮忙看一下啊
Java code

EmployeeMain.java
package com.dreamhome.file;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class EmployeeMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Employee[] staff=new Employee[3];
        staff[0]=new Employee("Tom", 562.12, 2012, 3, 21);
        staff[1]=new Employee("Tony", 4568, 2012, 2, 21);
        staff[2]=new Employee("hu", 562.12, 2012, 1, 21);
        try {
            PrintWriter out=new PrintWriter("employee.txt");
            writeDate(staff, out);
            out.close();
            Scanner in=new Scanner(new FileReader("employee.txt"));
            Employee[] newStaff=readDate(in);
            in.close();
            for(Employee e : newStaff){
                System.out.println(e);
            }
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在");
        }
    }
    
    private static void writeDate(Employee[] employees,PrintWriter out){
        out.println(employees.length);
        for (Employee e:employees) {
            e.writeData(out);
        }
    }
    
    private static Employee[] readDate(Scanner in){
        int n=in.nextInt();
        in.nextLine();
        Employee[] employees=new Employee[n];
        for (int i = 0; i < n; i++) {
            employees[i]=new Employee();
            employees[i].readData(in);
        }
        return employees;
    }
}





------解决方案--------------------
Employee.java中的writeData(PrintWriter out)方法中的System.out改成out