简体   繁体   English

无法从Java中的二进制文件读取对象

[英]Trouble reading objects from binary file in Java

I'm trying to read the serialized data from a .dat file into its corresponding java class file. 我正在尝试将序列化数据从.dat文件读取到其相应的Java类文件中。 My .readObject() method keeps throwing ClassNotFoundException though. 我的.readObject()方法不断抛出ClassNotFoundException。 Why cant it read the data from the file? 为什么它不能从文件中读取数据? I'm trying to read it into a student class who's constructor accepts objects of its type then assigns the values of that object's variables to its variables. 我正在尝试将其读入一个学生类,该类的构造函数接受其类型的对象,然后将该对象的变量的值分配给其变量。

public class StudentTest 
{
    private static ObjectInputStream OIS;
    public static void main(String[] args)
    {

        openFile();
        readFile();
        closeFile();
    }

    public static void openFile()
    {
        try
        {
            OIS=new ObjectInputStream(Files.newInputStream(Paths.get("C:\\Users\\Joey\\Desktop\\students.dat")));
        }
        catch(IOException e)
        {
            System.out.println("Error trying to read file. TERMINATING.");
            System.exit(1);
        }
    }

    public static void readFile()
    {
        try
        {
            while(true)
            {
                Student student=new Student((Student)OIS.readObject());
                System.out.printf("%s", student.toString());
            }
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Class not found. Terminating.");
            System.exit(1);
        }
        catch(EOFException e)
        {
            System.out.println("End of file reached.");
        }
        catch(IOException e)
        {
            System.out.println("Error reading from file. TERMINATING.");
            System.exit(1);
        }
    }

    public static void closeFile()
    {
        try
        {
            if(OIS!=null)
                OIS.close();
        }
        catch(IOException e)
        {
            System.out.println("IOEXCEPTION.");
            System.exit(1);
        }
    }
} 

Here's the stack trace: 这是堆栈跟踪:

java.lang.ClassNotFoundException: files.Student
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at studentfiles.StudentTest.readFile(StudentTest.java:40)
at studentfiles.StudentTest.main(StudentTest.java:16)

Your Student class is in a different package from what is expected. 您的Student班与预期班次不同。 If you print the exception message that came with the ClassNotFoundException you will see what the expected package was. 如果您打印ClassNotFoundException附带的异常消息,您将看到预期的软件包。

And an error creating an ObjectInputStream or FileInputStream is not necessarily an 'error reading from file'. 创建ObjectInputStreamFileInputStream的错误不一定是“从文件读取错误”。 Don't make up your own error messages. 不要自己编写错误消息。 Use the one that comes with the exception. 使用该异常随附的一种。 Even when you have the right message, as with ClassNotFoundException , you're missing the actual class name that wasn't found, which was in the exception message. 即使您有正确的消息(如ClassNotFoundException ,您也会缺少在异常消息中找不到的实际类名。 Don't do this. 不要这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM