Java Reference
In-Depth Information
{
try (ObjectOutputStream oos =
new
ObjectOutputStream(new
FileOut-
putStream("employee.dat")))
{
SerEmployee se = new SerEmployee("John Doe");
System.out.println(se);
oos.writeObject(se);
System.out.println("se object written to file");
}
catch (Exception e)
{
e.printStackTrace();
}
try (ObjectInputStream ois =
new
ObjectInputStream(new
FileIn-
putStream("employee.dat")))
{
SerEmployee se = (SerEmployee) ois.readObject();
System.out.println("se object read from file");
System.out.println(se);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
SerEmployee 's writeObject() and readObject() methods rely on
DataOutput and DataInput methods: they don't need to call writeObject()
and readObject() to perform their tasks.
When you run this application, it generates the following output:
John Doe
se object written to file
Search WWH ::




Custom Search