1 / 19

Files and Serialization

Files and Serialization. Files. Used to transfer data to and from secondary storage. A Portion of Java ’ s File Classes. Object. InputStream. OutputStream. File. FileInputStream. ObjectInputStream. ObjectOutputStream. FileOutputStream.

ryo
Download Presentation

Files and Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files and Serialization

  2. Files Used to transfer data to and from secondary storage

  3. A Portion of Java’s File Classes Object InputStream OutputStream File FileInputStream ObjectInputStream ObjectOutputStream FileOutputStream These classes are included in the java.io package, which must be imported into any application that uses them

  4. Basic Algorithm for File Output • Open an output connection to a file • Write the data to the file • Close the output connection to the file Disk FileOutputStream

  5. Files and Exceptions • Java requires the programmer to catch file exceptions • For example, an IOException can be thrown during at attempt to open a file

  6. try-catch Format try{ <code that might throw an exception> }catch(<exception type-1> e){ <code to handle exception of type 1> } . . catch(<exception type-n> e){ <code to handle exception of type n> } The exception thrown by try is matched against the parameter of each catch in sequence.

  7. Writing Objects to Text Files Student s = new Student("Ken", 10); try{ FileOutputStreamfos = new FileOutputStream("student.txt"); PrintWriter writer = new PrintWriter(fos); writer.println(s.getName()); writer.println(s.getNumScores()); for (inti = 1; i <= s.getNumScores(); i++) writer.println(s.getScore(i)); fos.flush(); fos.close(); }catch(Exception e){ System.out.println("Error in output:" + e.toString()); } Must agree with input process about format of text file.

  8. Basic Algorithm for File Input • Open an input connection to a file • Read the data from the file and process it • Close the input connection to the file Disk FileInputStream

  9. Reading Objects from Text Files Student s = null; try{ FileInputStreamfis = new FileInputStream("student.txt"); Scanner reader = new Scanner(fis); String name = reader.nextLine(); intnumScores = Integer.parseInt(reader.nextLine()); s = new Student(name, numScores); for (i = 1; i <= numScores; i++) s.getScore(Integer.parseInt(reader.nextLine)); fis.close(); }catch(Exception e){ System.out.println("Error in output:" + e.toString()); } Must agree with output process about format of text file.

  10. A Simpler Solution: Serialization • A data model is said to be serialized when it can be permanently saved on a storage medium • Serialization requires file management • OOP systems support object serialization

  11. Serializing a Data Model • All classes to be serialized must implement the Serializable interface • This interface is defined in the java.io package • Many standard java classes are already serializable

  12. Serializing the Student Class import java.io.Serializable; public class Student implements Comparable<Student>, Serializable{ . . } Many of Java’s data classes are already serializable All specialized data classes should be made to be serializable

  13. Writing Objects to Object Files • Create a new instance of FileOutputStream • Wrap around this object a new instance of ObjectOutputStream • Use the method writeObject to send an object out on the stream

  14. Writing Objects to Output Files A file name new FileOutputStream(<a String>) new ObjectOutputStream(<a FileOutputStream>) <an ObjectOutputStream>.writeObject(<any object>)

  15. Writing Objects to Output Files Student s = new Student("Ken", 10); try{ FileOutputStreamfos = new FileOutputStream("student.dat"); ObjectOutputStreamoos = new ObjectOutputStream(fos); oos.writeObject(s); fos.flush(); fos.close(); }catch(Exception e){ System.out.println("Error in output:" + e.toString()); } Writes the whole Student object to the file without any fuss.

  16. Reading Objects from Input Files • Create a new instance of FileInputStream • Wrap around this object a new instance of ObjectInputStream • Use the method readObject to receive an object from the stream • Cast the received object to its actual class

  17. Reading Objects from Input Files A file name new FileInputStream(<a String>) new ObjectInputStream(<a FileInputStream>) (<class name>) <an objectInputStream>.readObject()

  18. Reading Objects from Input Files Student s; try{ FileInputStreamfis = new FileInputStream("student.dat"); ObjectInputStreamois = new ObjectInputStream(fis); s = (Student) ois.readObject(); fis.close(); }catch(Exception e){ System.out.println("Error in input:" + e.toString()); } Reads the whole Student object from the file without any fuss.

  19. Define a static Method for Input static public readFromFile(ObjectInputStreamois){ try{ return (Student) ois.readObject(); }catch(Exception e){ System.out.println("Error in input:" + e.toString()); return null; } } try{ // Open file input stream and object input stream // while there are more objects to be read studentList.add(Student.readfromFile(ois)); } catch(Exception e){ … Alternatively, write and read the entire list!

More Related