您好,我正在学习泛型,但我在将对象从文件反序列化回泛型列表然后打印时遇到问题。
结果是一个空屏幕,即使我知道创建了一个 dat 文件
我尝试过很多种方法。
我使用带有 ActionListener 的框架..
这是 ActionListener
private class AddButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// variables
String inputfName;
String inputlName;
String idInput;
//String studentID;
// capture input
inputfName = sFirstName.getText();
inputlName = sLastName.getText();
idInput = sID.getText();
//hold Student data
//convert to Int
//studentID = Integer.parseInt(idInput);
//Create file
StudentAddProcess sFile = new StudentAddProcess();
sFile.writesFile(inputfName, inputlName, idInput );
}
这些处理输入
公共(public)类 StudentAddProcess 实现可序列化{
String inputFirstName ;
String inputLastName;
String inputID;
private Component Frame;
// Create File
public void writesFile ( String fName, String lName, String Id ){
// create T fields for student
inputFirstName = fName;
inputLastName = lName;
inputID = Id;
try {
ObjectOutputStream objectInputFile = new ObjectOutputStream(outStream);
//Linkeked List of Student Objects
GenericLinkedList<Student> studentList = new GenericLinkedList<>();
// add Student Object to student
studentList.add(new Student(inputID, inputFirstName, inputLastName));
//Where am i
JOptionPane.showMessageDialog(Frame, "writing ...");
// for loop cycle and write student to fill
for (int i =0 ; i < studentList.size();i++) {
objectInputFile.writeObject(studentList.get(i));
}
JOptionPane.showMessageDialog(Frame, "Complete :");
//close file
objectInputFile.close();
现在我们查看添加的对象
public class StudentViewFile implements Serializable {
//variables
private String studentID; // holds passed user request to view
private Component Frame;
public void readFile(String sID) throws IOException {
studentID = sID; // take passed variable
//int studentID2 =Integer.getInteger(sID);
try {
//open read file
FileInputStream inStream = new FileInputStream("StudentObjects.dat");// Stream of Objects
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
GenericLinkedList <Student> readRecord = new GenericLinkedList<>();
readRecord.add(new Student(studentID));
for (int i = 0; i < readRecord.size(); i++) {
readRecord = (GenericLinkedList) objectInputFile.readObject();
// Diplay records
System.out.println("File Objects" + readRecord.get(i) + " \n next positions");
// was going to try to print my test here
System.out.println(readRecord.toString());
}
// Read the serialized objects from the file.
while (true) {
readRecord = (GenericLinkedList) objectInputFile.readObject();
// Display records
System.out.println( "File Objects" + readRecord+ " \n next positions");
//close file
objectInputFile.close();
}
} catch (EOFException e) {
System.out.print("\n Exception caught ");
任何想法都会很棒。预先感谢您的时间和知识
最佳答案
将编写对象的方式与尝试读取对象的方式进行比较。
for (int i =0 ; i < studentList.size();i++) {
objectInputFile.writeObject(studentList.get(i));
}
这会写入一系列 Student
对象。
readRecord = (GenericLinkedList) objectInputFile.readObject();
这会尝试读取某些内容并将其转换为GenericLinkedList
。但它不会是其中之一。它实际上是一个Student
,所以你会得到一个异常。
另一个问题是:
GenericLinkedList <Student> readRecord = new GenericLinkedList<>();
readRecord.add(new Student(studentID));
for (int i = 0; i < readRecord.size(); i++) {
readRecord = (GenericLinkedList) objectInputFile.readObject();
你到底想绕圈多少次?这个数字实际上是从哪里来的?
有多种方法可以解决这个问题;例如:
- 将整个列表作为单个对象写入(然后读取)
- 通过将列表大小 (
studentList.size()
) 写入int
来启动流。读取时,首先读取列表大小,然后读取Student
对象的数量。 - 使用
while (true)
循环读取并捕获/处理EOFException
。
(我注意到您已尝试按照损坏的代码执行替代方案 3 ...。当然,这不会超越您损坏的代码,因此这是行不通的。是什么你在想吗?)
关于java - java通用对象反序列化和序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083047/