【问题标题】:Unhandled ClassNotFound Exception when reading object读取对象时出现未处理的 ClassNotFound 异常
【发布时间】:2014-08-25 06:33:05
【问题描述】:

所以,我试图从文件中读取一个对象,但我不知道为什么会出现这个异常,或者如何解决它。也许你们可以帮助我?我试过弄乱我阅读对象的方式,但不能完全正确。这是我的代码,我在读取 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;
    }
}

【问题讨论】:

  • 总是发布堆栈跟踪。这通常表明您没有可用的库,其中包含您尝试读取的流中的某些对象的类定义。
  • 您是否在抱怨编译错误?

标签: java exception objectinputstream classnotfound


【解决方案1】:

根据 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();
        }

【讨论】:

  • 这非常简单。谢谢。哈哈
  • @ScaryWombat,它是一个检查异常,需要在代码中处理。所以它应该解决OP发布的问题,我在这里遗漏了什么吗?
  • 哇,我重新阅读了 OP 问题,但我仍然无法弄清楚他在抱怨代码无法编译。
  • 是的,这是一个编译错误,修复它运行程序没有问题。
  • @user2065083 是的......因为现在你的程序忽略了这个问题并继续前进,即使错误仍在发生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多