读取对象时未处理的ClassNotFound异常

时间:2014-08-25 06:33:06

标签: java exception objectinputstream classnotfound

所以,我正在尝试从文件中读取一个Object,我无法弄清楚为什么我会收到此异常,或者如何修复它。也许你们可以帮助我吗?我已经尝试过阅读对象的方式了,但是不能正确地理解它。这是我的代码我在读取listOfEmployeesIn [i] =(Employee)objIn.readObject();

的行上得到错误
import java.util.Random;
import java.io.*;

public class ProjectFive{
    public static void main(String[] args) throws IOException{
        Random rn = new Random();
        RandomAccessFile file = new RandomAccessFile("employees.txt", "rw");
        FileOutputStream fileOut = new FileOutputStream("employees.txt");
        ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
        FileInputStream fileIn = new FileInputStream("employees.txt");
        ObjectInputStream objIn = new ObjectInputStream(fileIn);
        Object x;

        long SSN;
        float salary;
        int age;
        float maxSalary = 200000;
        float minSalary = 20000;
        long SSNRange = 1000000000;

        String[] names = {"Matty Villa"};
        Employee[] listOfEmployeesOut = new Employee[20];
        Employee[] listOfEmployeesIn = new Employee[20];

        for(int i=0;i<listOfEmployeesOut.length;i++){
            SSN = (long)(rn.nextDouble()*SSNRange);
            salary = rn.nextFloat()*(maxSalary - minSalary)+minSalary;
            age = rn.nextInt(57)+18;
            listOfEmployeesOut[i] = new Employee(SSN, names[i], salary, age);
        }
        for(int i = 0;i<listOfEmployeesOut.length;i++){
            objOut.writeObject(listOfEmployeesOut[i]);
        }
        for(int i = 0;i<listOfEmployeesIn.length;i++){
            listOfEmployeesIn[i] = (Employee) objIn.readObject();
        }
        file.close();
        fileOut.close();
        objOut.close();
        fileIn.close();
        objIn.close();


    }
}   
class Employee implements Serializable{
    public long socialSecurityNumber;
    public String fullName;
    public float salary;
    public int age;

    public Employee(long socialSecurityNumber, String fullName, float salary, int age){
        this.socialSecurityNumber = socialSecurityNumber;
        if(fullName.length() != 50){
            fullName = resizeString(fullName);
        }
        this.fullName = fullName;
        this.salary = salary;
        this.age = age;
    }

    private String resizeString(String s){
        if(s.length() < 50){
            for(int i = s.length(); i<=50; i++){
                s += ' ';
            }
        }else{
            s = s.substring(0,50);
        }
        return s;
    }
    public String toString(){
        String out = "Name: " + fullName + "\nSalary: " + salary + "\nSocial: " + socialSecurityNumber
            + "\nAge: " + age;
        return out;
    }
}

1 个答案:

答案 0 :(得分:0)

根据ObjectInputStream的JAVA API,方法readObject会抛出已检查的异常 - IOException, ClassNotFoundException

因此要么从main方法抛出此异常:

public static void main(String[] args) throws IOException, ClassNotFoundException

或使用try/catch块处理它:

        try {
            listOfEmployeesIn[i] = (Employee) objIn.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }