I am trying to read an object via ObjectInputStream
. However I retrieve the following stacktrace with an EOFException
:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at student.operations.StudentOperations.addStudentDetails(StudentOperations.java:46)
at student.
My code is the following:
FileInputStream fi = new FileInputStream(file.getPath());
ObjIn = new ObjectInputStream(fi);
FileOutputStream fo = new FileOutputStream(file.getPath());
Objout = new ObjectOutputStream(fo);
//bo = new BufferedOutputStream(Objout);
//bi = new BufferedInputStream(ObjIn);
System.out.println("Enter the number of students to add");
number = in.nextInt();
System.out.println("Enter Student roll number:");
int rollno = in.nextInt();
try {
HashMap<Integer, StudentModel> hs = (HashMap<Integer, StudentModel>) ObjIn
.readObject();
if (hs.containsKey(rollno)) {
System.out.println("Duplicate values are not allowed ");
} else {
for (counter = 0; counter < number; counter++) {
System.out.println("Enter Student name:");
String name = in.next();
System.out.println("Enter Student's Father name:");
String fname = in.next();
System.out.println("Enter Gender:");
String gender = in.next();
System.out.println("Enter Date of birth:");
String date = in.next();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MM-yyyy");
Date dateObj = null;
try {
dateObj = dateFormat.parse(date);
} catch (ParseException e) {
System.out.println(e);
}
Calendar birthday = Calendar.getInstance();
birthday.setTimeInMillis(dateObj.getTime());
Calendar current = Calendar.getInstance();
long currentTime = System.currentTimeMillis();
current.setTimeInMillis(currentTime);
int age = current.get(Calendar.YEAR)
- birthday.get(Calendar.YEAR);
System.out.println("Enter the AddressLine1:");
String address1 = in.next();
in.nextLine();
System.out.println("Enter the AddressLine2:");
String address2 = in.next();
in.nextLine();
System.out.println("Enter the city");
String city = in.next();
in.nextLine();
System.out.println("Enter the state");
String state = in.next();
in.nextLine();
System.out.println("Enter the country:");
String country = in.next();
in.nextLine();
System.out.println("Enter the Zipcode");
int code = in.nextInt();
in.nextLine();
StudentUtill.student.put(rollno, new StudentModel(name,
fname, rollno, age, gender, dateObj, address1,
address2, city, state, country, code));
Objout.writeObject(StudentUtill.student.toString());
Objout.flush();
}// for loop ends here
tester.StudentTester.main(StudentTester.java:30)
You're trying to read objects from an empty file. Look at the stack trace. End of file trying to read the header. There isn't even a header in the file, let alone an object. It's empty.
Don't create a FileOutputStream
until you have something to write to it, and don't forget to close it immediately afterwards.
You're also incorrectly converting the map to a String
before writing it to the file, but you haven't yet got to the point where that's a problem.