• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oop lecture9 12
 

Oop lecture9 12

on

  • 373 views

 

Statistics

Views

Total Views
373
Views on SlideShare
373
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Oop lecture9 12 Oop lecture9 12 Presentation Transcript

    • Lecture 12Serializable Interface Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
    • What is serialization? Example: we want to store an object on the disk and recover laterpublic 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");
    • try{ Rest of the code 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();}}}
    • Output 9 John 34 Road, DhanmondiLesson learned: we can save an object and retrieve if later ifimplement java.io.Serializable interface
    • 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); //writing File 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); } }}
    • OutputAlso SerializableDemo2.ser file is created in the disk
    • 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 afterreconsructing from serializedobject
    • Exception handlingtry-catch-finallypublic 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 notavailable"); }finally{ try{ fis.close(); }catch(Exception ex){ //nothing to do } } }}
    • Details about exceptionsAt 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 clauseFinally clause is not mandatoryExceptions can be extended just time any other java class
    • More on exceptionsIf some code in a method raises some exception we must handle it either by wrapping the code with try-catch blockAlternatively, 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
    • throws clauseThis 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); }