About Java throws java.io.EOFException exception handling method when deserializing stream

At the beginning, when deserializing the stream, an EOFException was thrown;

Processing method:

Does the custom class implement the Serializable interface?

When deserializing the stream, you need to force the ovobject to the custom class

Student obj = (Student) ois.readObject();






import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;


public class Test {


public static void main(String[] args) throws  Exception {
File f = new File("sava.txt"); if(f.exists()){ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f)); Student obj = (Student) ois.readObject(); System.out.println( obj); ois.close(); }else{ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));













Scanner sc = new Scanner(System.in);
Student s = new Student(); System.out.println("Please enter the student's name");     s.setName(sc.next()); System.out.println( "Please enter the student ID number");     s.setId(sc.nextInt()); System.out.println("Please enter the student ID number"); s.setIdcardnum(sc.nextInt()); System.out .println("Please enter the student's math score"); s.setMathcj(sc.nextDouble()); System.out.println("Please enter the student's Java score"); s.setJavacj(sc.nextDouble()) ; System.out.println("Please enter the student's English grade"); s.setEnglishcj(sc.nextDouble()); oos.writeObject(s); oos.close(); } }



















}




public class Student implements Serializable {
/**

*/
private static final long serialVersionUID = -8763490558256462498L;
private int id;
private String name;
transient private int idcardnum;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", javacj=" + javacj + ", mathcj=" + mathcj + ", englishcj="
+ englishcj + "]";
}
public Student(int id, String name, int idcardnum, double javacj, double mathcj, double englishcj) {
super();
this.id = id;
this.name = name;
this.idcardnum = idcardnum;
this.javacj = javacj;
this.mathcj = mathcj;
this.englishcj = englishcj;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdcardnum() {
return idcardnum;
}
public void setIdcardnum(int idcardnum) {
this.idcardnum = idcardnum;
}
public double getJavacj() {
return javacj;
}
public void setJavacj(double javacj) {
this.javacj = javacj;
}
public double getMathcj() {
return mathcj;
}
public void setMathcj(double mathcj) {
this.mathcj = mathcj;
}
public double getEnglishcj() {
return englishcj;
}
public void setEnglishcj(double englishcj) {
this.englishcj = englishcj;
}
private double javacj;
private double mathcj;
private double englishcj;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324525163&siteId=291194637
Recommended