我正在尝试反序列化文件以显示我存储的内容而不是它存储的字节 - 任何帮助?
public Object getObject(String obj) throws IOException, ClassNotFoundException{
// Create the stream objects.
String file = "files\\" + obj + ".dat";
FileInputStream inStream = new FileInputStream(file);
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
ArrayList<Object> records = new ArrayList<Object>();
// Read the serialized objects from the file.
try{
records.add(objectInputFile.readObject().toString());
}catch(EOFException e){
System.out.println("ERROR " + e);
}
// Close the file.
objectInputFile.close();
// Display the objects.
System.out.println(records);
return records;
}
运行时,输出
[[[[[Staff@41e1d4bd], Staff@34741df1]], Staff@441f8094]]
如何将此字节转换为文本?
编辑:
经过一些更改后,我的代码现在出错了:
public Staff getObject(String obj) throws IOException, ClassNotFoundException{
// Create the stream objects.
String file = "files\\" + obj + ".dat";
FileInputStream inStream = new FileInputStream(file);
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
Staff records = null;
// Read the serialized objects from the file.
try{
records = (Staff)objectInputFile.readObject();
}catch(EOFException e){
System.out.println("ERROR " + e);
}
// Close the file.
objectInputFile.close();
// Display the objects.
System.out.println(records);
return records;
}
新错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to Staff