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();
}
}
}
Student Class
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;
}
}
Person class
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;
}
}
errors
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)
for some unknown reason, my FileInputStream is not working. The file is getting created, so I know the FileOutputStream is working. But, when reading the file it can't decipher the meaning.
Studentclass.FileInputStreambutObjectInputStream. And when you look at the docs forInvalidClassException, you will know what to look for in your code. Or you could just follow Erik Kralj's advice.Serializableso I need to use Student. However, I believe theFileInputStreamis storing my Student a as a String