Java Swing Layout Manager Event Handling Inner Classes Swing Applet Exceptions Threads Java I/O![]() Stream![]() Character Stream classes![]() Byte Stream classes![]() I/O Methods![]() Type of I/O![]() File Streams![]() Data Streams![]() Object Serialization![]() Writing and Read Object![]() Drawing Editor Revisit![]() Drawing Editor Revisit![]() The Draw Class![]() The Graphic Element Classes![]() The Element Implementation classes![]() Transient Fields Network Programming Resources | Writing and Read ObjectTo actually persist the object, use java.io.ObjectOutputStream class: FileOutputStream ostream = new FileOutputStream("t.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeInt(12345);
p.writeObject("Today");
p.writeObject(new Date());
p.flush();
ostream.close(); To read back what you have persisted, use the java.io.ObjectInputStream class: FileInputStream istream = new FileInputStream("t.tmp");
ObjectInputStream p = new ObjectInputStream(istream);
int i = p.readInt();
String today = (String)p.readObject();
Date date = (Date)p.readObject();
istream.close(); |