Oop lecture9 12

11
Serializable Interface Lecture 12 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Transcript of Oop lecture9 12

Page 1: Oop lecture9 12

Serializable Interface

Lecture 12

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture9 12

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

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

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

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

Output

Also SerializableDemo2.ser file is created in the disk

Page 7: Oop lecture9 12

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

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

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

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

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);}