Your SlideShare is downloading. ×
Oop lecture9 12
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Oop lecture9 12

328

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
328
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Lecture 12Serializable Interface Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. 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");
  • 3. 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();}}}
  • 4. Output 9 John 34 Road, DhanmondiLesson learned: we can save an object and retrieve if later ifimplement java.io.Serializable interface
  • 5. 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); } }}
  • 6. OutputAlso SerializableDemo2.ser file is created in the disk
  • 7. 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
  • 8. 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 } } }}
  • 9. 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
  • 10. 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
  • 11. 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); }

×