Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Files and Serialization. Files Used to transfer data to and from secondary storage."— 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 InputStreamOutputStream FileInputStreamFileOutputStreamObjectInputStreamObjectOutputStream File 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 FileOutputStream Disk

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{ }catch( e){ }. catch( e){ } 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{ FileOutputStream fos = new FileOutputStream("student.txt"); PrintWriter writer = new PrintWriter(fos); writer.println(s.getName()); writer.println(s.getNumScores()); for (int i = 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 FileInputStream Disk

9 Reading Objects from Text Files Student s = null; try{ FileInputStream fis = new FileInputStream("student.txt"); Scanner reader = new Scanner(fis); String name = reader.nextLine(); int numScores = 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 import java.io.Serializable; public class Student implements Comparable, Serializable{. } Serializing the Student Class 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 new FileOutputStream( ) new ObjectOutputStream( ).writeObject( ) A file name

15 Writing Objects to Output Files Student s = new Student("Ken", 10); try{ FileOutputStream fos = new FileOutputStream("student.dat"); ObjectOutputStream oos = 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 new FileInputStream( ) new ObjectInputStream( ) ( ).readObject() A file name

18 Reading Objects from Input Files Student s; try{ FileInputStream fis = new FileInputStream("student.dat"); ObjectInputStream ois = 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(ObjectInputStream ois){ 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!


Download ppt "Files and Serialization. Files Used to transfer data to and from secondary storage."

Similar presentations


Ads by Google