oop lecture9 12
TRANSCRIPT
![Page 1: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/1.jpg)
Serializable Interface
Lecture 12
Object Oriented ProgrammingEastern University, Dhaka
Md. Raihan Kibria
![Page 2: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/2.jpg)
What is serialization?
Example: we want to store an object on the disk and recover later
public class SerializableDemo implements Serializable {
Integer id;String name;String address;
public static void main(String[] args) {
SerializableDemo s = new SerializableDemo();s.id= new Integer(9);s.name = "John";s.address = "34 Road, Dhanmondi";
//writingFile f = new File("/home/user/eu/oop/SerializableDemo.ser");
![Page 3: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/3.jpg)
Rest of the codetry{ ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f)); o.writeObject(s); o.close(); }catch(Exception e){ e.printStackTrace(); System.exit(0);}
//readingtry{ ObjectInputStream i = new ObjectInputStream(new FileInputStream(f)); SerializableDemo d = (SerializableDemo)i.readObject(); i.close(); System.out.println(d.id); System.out.println(d.name); System.out.println(d.address);}catch(Exception e){ e.printStackTrace();}}}
![Page 4: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/4.jpg)
Output
9John34 Road, Dhanmondi
Lesson learned: we can save an object and retrieve if later if implement java.io.Serializable interface
![Page 5: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/5.jpg)
Another serializable demopublic class SerializableDemo2 extends JFrame{
SerializableDemo2(){this.setBounds(100, 100, 400, 300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.add(new JButton("Test button"));
}
public static void main(String[] args) {SerializableDemo2 s = new SerializableDemo2();s.setVisible(true);//writingFile f = new File("/home/user/eu/oop/SerializableDemo2.ser");
try{ ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f)); o.writeObject(s); o.close();}catch(Exception e){ e.printStackTrace(); System.exit(0);}
}}
![Page 6: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/6.jpg)
Output
Also SerializableDemo2.ser file is created in the disk
![Page 7: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/7.jpg)
We want to read the serialized object by another program
//readingFile f = new File("/home/user/eu/oop/SerializableDemo2.ser");try{ ObjectInputStream i = new ObjectInputStream(new FileInputStream(f)); SerializableDemo2 d = (SerializableDemo2)i.readObject(); d.setVisible(true);}catch(Exception e){ e.printStackTrace();}
The same frame shows after reconsructing from serialized object
![Page 8: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/8.jpg)
Exception handling
try-catch-finally
public class TryCatchDemo { public static void main(String[] args) {
File file = new File("/home/user/tmp");FileInputStream fis = null;try{
fis = new FileInputStream(file);}catch(FileNotFoundException e){
System.out.println("The file you are trying to access is not available");
}finally{try{
fis.close();}catch(Exception ex){//nothing to do}
} }}
![Page 9: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/9.jpg)
Details about exceptions
At first code in the try clause is executedIf there is any exception code in Exception
clause is executedFinally clause is executed after the try clause
is executed. We close any resources in the finally clause
Finally clause is not mandatory
Exceptions can be extended just time any other java class
![Page 10: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/10.jpg)
More on exceptions
If some code in a method raises some exception we must handle it either by wrapping the code with try-catch block
Alternatively, you can re-raise the exception by using throws clause in the method
public void doSomething(){ File file = new File("/home/user/a.out"); FileInputStream fis = null; fis = new FileInputStream(file);}
//this code will not compile
![Page 11: Oop lecture9 12](https://reader036.vdocuments.mx/reader036/viewer/2022062405/555c3d9bd8b42a2c068b4b6b/html5/thumbnails/11.jpg)
throws clause
This will compile; but the caller must handle the excepion or declare its own throws
public void doSomething() throws FileNotFoundException{ File file = new File("/home/user/a.out"); FileInputStream fis = null; fis = new FileInputStream(file);}