package edu.uga.cs1302.gui;
import javax.swing.SwingUtilities;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class StudentMain {
public static void main(String[] args) {
// this is inside a "lambda" Runnable
FileOutputStream fos;
try {
Student a = new Student("C", "H", 1234, "12-11-1937", "UGA");
fos = new FileOutputStream("StudentsList.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
a.storeObject(oos);
System.out.println("success");
FileInputStream fis = new FileInputStream("StudentsList.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Student b = new Student("B", "H", 8888, "10-22-1936", "UNC");
b.retrieveObject(ois);
System.out.println(b.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
学生班
package edu.uga.cs1302.gui;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.ParseException;
public class Student extends Person implements Serializable{
protected String college;
private static final long serialVersionUID = -5047600810798119921L;
public Student(String firstname, String lastname,int ID, String DOB1, String college) throws ParseException{
super(firstname, lastname,ID, DOB1);
this.college=college;
}
public void storeObject(ObjectOutputStream out) throws IOException{
out.writeObject(this);
out.flush();
}
public void retrieveObject(ObjectInputStream in) throws IOException, ClassNotFoundException, ParseException{
Student temp = (Student) in.readObject();
System.out.println(temp.toString());
this.setFirst(temp.getFirst());
this.setLast(temp.getLast());
this.setID(temp.getID());
this.setDOB(temp.getDOB());
this.setCollege(temp.getCollege());
}
public String getCollege(){
return college;
}
public void setCollege(String college){
this.college= college;
}
public String toString(){
String student = super.getID() + " "+super.getFirst()+" "+super.getLast()+" "+super.getDOB()+" ["+college+"]";
return student;
}
}
人类
package edu.uga.cs1302.gui;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Person {
public String first;
public String last;
public Date date1;
public int ID;
public String DOB;
public Person(String firstname, String lastname,int ID, String DOB1) throws ParseException{
this.first=firstname;
this.last=lastname;
if(ID>999&&ID<10000){
this.ID = ID;
}
SimpleDateFormat simple = new SimpleDateFormat("MM-dd-yyyy");
Date date = simple.parse(DOB1); //must parse to get data
this.date1 = date;
this.DOB = DOB1;
//this.DOB = DOB;
}
public String getFirst(){
return first;
}
public String getLast(){
return last;
}
public String getDOB(){
return DOB;
}
public int getID(){
return ID;
}
public void setFirst(String first){
this.first = first;
}
public void setLast(String last){
this.last = last;
}
public void setDOB(String DOB1) throws ParseException{
SimpleDateFormat simple = new SimpleDateFormat("MM-dd-yyyy");
Date date = simple.parse(DOB1); //must parse to get data
this.date1 = date;
}
public void setID(int ID){
this.ID = ID;
}
public String toString(){
SimpleDateFormat sim = new SimpleDateFormat("MM-dd-yyyy");
String date = sim.format(this.date1);//formats to simple format
String person = ""+ ID + " "+first+" "+last+" "+date;
return person;
}
}
错误
success
java.io.InvalidClassException: edu.uga.cs1302.gui.Student; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:790)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1775)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at edu.uga.cs1302.gui.Student.retrieveObject(Student.java:25)
at edu.uga.cs1302.gui.StudentMain.lambda$0(StudentMain.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
由于某些未知原因,我的FileInputStream不起作用。文件正在创建,所以我知道FileOutputStream正在运行。但是,在阅读文件时,它无法破译其含义。
答案 0 :(得分:1)
如果你有一个不具有无参数构造函数的非可序列化类,你很可能在序列化期间获得InvalidClassException
,然后在任何扩展此calss的类中反序列化。
解决此问题的方法之一是使用序列化代理模式。
要实现它,您必须在实现Serializable的类中创建一个静态嵌套的SerializationProxy类。 SerializationProxy将表示封闭类的逻辑状态,并将在序列化期间写入字节流 而不是原始实例,并且在反序列化期间将创建原始类的实例,该实例在对象图中占据一席之地。
https://dzone.com/articles/serialization-proxy-pattern
http://blog.codefx.org/design/patterns/serialization-proxy-pattern/
答案 1 :(得分:0)
您需要将所有对象都可序列化。只需实现Serializable for Person以便可以编写它。实际上,你应该将你的基类标记为可序列化,然后子类将由于继承而自动序列化。
成功 1234 C H 12-11-1937 [UGA] 1234 C H 10-22-1936 [UGA]