无法从 Java 中的二进制文件中读取对象
Trouble reading objects from binary file in Java
我正在尝试将序列化数据从 .dat 文件读取到相应的 java class 文件中。我的 .readObject() 方法不断抛出 ClassNotFoundException。为什么它不能从文件中读取数据?我正在尝试将它读给一个学生 class,他的构造函数接受其类型的对象,然后将该对象的变量的值分配给它的变量。
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);
}
}
}
这是堆栈跟踪:
java.lang.ClassNotFoundException: files.Student
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.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)
您的 Student
class 与预期的包裹不同。如果您打印 ClassNotFoundException
附带的异常消息,您将看到预期的包是什么。
并且创建 ObjectInputStream
或 FileInputStream
的错误不一定是 'error reading from file'。不要自己编造错误信息。使用异常附带的那个。即使您有正确的消息,如 ClassNotFoundException
,您也遗漏了未找到的实际 class 名称,该名称位于异常消息中。不要这样做。
我正在尝试将序列化数据从 .dat 文件读取到相应的 java class 文件中。我的 .readObject() 方法不断抛出 ClassNotFoundException。为什么它不能从文件中读取数据?我正在尝试将它读给一个学生 class,他的构造函数接受其类型的对象,然后将该对象的变量的值分配给它的变量。
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);
}
}
}
这是堆栈跟踪:
java.lang.ClassNotFoundException: files.Student
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.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)
您的 Student
class 与预期的包裹不同。如果您打印 ClassNotFoundException
附带的异常消息,您将看到预期的包是什么。
并且创建 ObjectInputStream
或 FileInputStream
的错误不一定是 'error reading from file'。不要自己编造错误信息。使用异常附带的那个。即使您有正确的消息,如 ClassNotFoundException
,您也遗漏了未找到的实际 class 名称,该名称位于异常消息中。不要这样做。