I'm trying to implement object serialization in java. I'm not sure why my code is currently leading to an EOFException, as my code is writing to the file it's supposed to:
Currently, my code is:
import java.io.*;
public class main {
public static void main(String[] args) {
product p1 = new product("product", 0, "this is a product.");
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("info.dat")));
out.writeObject(p1);
out.writeObject(p1);
System.out.println("Wrote to file");
} catch (FileNotFoundException e) {
System.out.println("");
} catch (IOException e) {
System.out.println("");
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("info.dat")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while product
class is extremely simple, being only:
import java.io.Serializable;
public class product implements Serializable {
String name;
double cost;
String description;
public product(String a,double b,String c) {
name = a;
cost = b;
description = c;
}
}
Meaning that my code did run and did write to file. I also added an extra write object just in case, so I really don't understand how an EOF error is occurring. Can somebody please explain why?