24-02-25 ----------- Q)Modify the above Java application(ReadingFromFile.java) so as to implement exception handling. Note:- Exception handling is implemented in the following application. file is closed in finally block. //ReadingFromFile.java import java.io.FileReader; import java.io.IOException; import java.io.FileNotFoundException; class ReadingFromFile{ public static void main(String[] args) { FileReader fileReader=null; try{ fileReader=new FileReader("one.txt"); int c=fileReader.read(); while(c !=-1){ System.out.print((char)c); c=fileReader.read(); } }//try catch(FileNotFoundException e){ System.out.println("Speciifed file not found"); } catch (IOException e){ System.out.println(e); } finally{ try{ if(fileReader!=null){ fileReader.close(); } } catch(IOException e){ System.out.println(e); } } }//main }//class Note:- usage of finally is mandatory but causing serious coding inconvenience. To overcome this probelm. "try with resources" is given by Java exception handling mechanism. import java.io.FileReader; import java.io.IOException; import java.io.FileNotFoundException; class ReadingFromFile{ public static void main(String[] args) { try(FileReader fileReader=new FileReader("one.txt")){ int c=fileReader.read(); while(c !=-1){ System.out.print((char)c); c=fileReader.read(); }//while }//try catch(FileNotFoundException e){ System.out.println("Speciifed file not found"); } catch (IOException e){ System.out.println(e); } }//main }//class Note:- try with resources replaces finally block. =>The try-with-resources statement is a try statement that declares one or more resources. =>A resource is an object that must be closed after the program is finished with it. =>The try-with-resources statement ensures that each resource is closed at the end of the try statement. =>Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. Q)What is serialization? =>serialization is a mechansim of converting an object into a stream of bytes so that either it can be stored into a disk file or exported to a remote machine. Q)What is persistence in the context of an object? =>The mechansim of storing an object into a disk file is nothig but persistence. =>Without serialization, an object can't be persisted. Q)Develop a Java application in which, an object is persisted into a disk file. import java.io.*; class Employee implements Serializable{ int empno; String name; Employee(int empno,String name){ this.empno=empno; this.name=name; System.out.println("object created and initialized"); } void displayEmployeeData(){ System.out.println("EMPNO:"+empno); System.out.println("NAME:"+name); } } class PersistenceExample{ public static void main(String[] args) throws IOException { Employee e=new Employee(101,"Rama"); FileOutputStream fos=new FileOutputStream("emp.data");//binary output stream ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(e); System.out.println("Object is stored into hard disk"); } }