Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming

2 Serialization Objects should provide methods to automatically read/write themselves to/from files “ write ” method: –Take filename as parameter –Write all member variables to file “ read ” method: –Take filename as parameter –Read data from file –Write that data into member variables Overloaded constructor automatically constructs object from file by calling read

3 Serialization public class NameList { … public void read(String file) { … } public void write(String file) { … } … public NameList(String file) { read(file); } file Example: Serializers for NameList class Overloaded constructor NameList object max count names read write

4 Streams Files accessed using streams –Accessed via operating system Have input and output stream objects –Have append option for output streams –Can represent any information source/sink (such as network) Program Input stream Output stream Operating System <<< data <<< >>> data >>>

5 Stream Types Data Streams/Files –Data stored/read/written in binary form –Useful for fixed size data (numbers, booleans, etc.) short x = 13;  Character Streams/Files –Data stored/read/written as text (list of characters) May require conversion between ASCII files and Java Unicode characters String s = “Fred”  0000000000001101 Fred

6 Streams and Pipelining Often connect “pipeline” of objects to process information between program and file Example: Printing string to character file Program out PrintWriter object Handles output an entire line at a time FileWriter object Handles output one character at a time, converting Unicode to ASCII file ASCII chars Individual Unicode chars String of Unicode chars

7 Exception Handling All file access must be inside try/catch blocks –Many potential problems Input file does not exist Output file on inaccessible device Contact lost while reading/writing Binary data in wrong format End of file reached… Must catch potential IOException object

8 Basic Syntax Create object(s) to represent link to file –Will involve nesting if pipelining used try { PrintWriter out = new PrintWriter(new FileWriter(“filename”)); … } catch (IOException ex) {…} link to file PrintWriter connects to a FileWriter FileWriter connects to some ASCII file

9 Basic Syntax Use methods to read/write file try { … out.println(“Hello world”); … } catch (IOException ex) {…} call method on stream object

10 Basic Syntax May not know how many values to read from file Can run until get end of file exception EOFException try { BufferedReader in = new BufferedReader(new FileReader(“file”)); try { while(true); String s = in.readLine(); // Process String s } catch (EOFException ex) {} } catch (IOException ex) {…} Loop until end of file exception thrown

11 Character Input/Output Streams Input: –BufferedReader : reads entire line from text input –String readLine() method used –FileReader used to connect to file Converts ASCII in files to Unicode characters used in java strings Output: –PrintWriter : sends entire line to text output –void println(String) method used –FileWriter used to connect to file

12 Using Character Streams Simplest method: Place each piece of data on separate line –Easier for println/readLine to get one line at a time –Otherwise, must parse line into separate data based on spaces Will need to parse numeric values read in Example NameList file: –maximum: 5 –current: 3 –names: [“Larry”, “Curley”, “Moe”] 5 3 Larry Curley Moe

13 NameList Serializer public void write(String filename) { try { PrintWriter out = new PrintWriter( new FileWriter(filename)); out.println(""+maximum); out.println(""+current); for (int i = 0; i < current; i++) { out.println(names[i]); } catch(IOException ex) { System.out.println("Could not open "+filename); } Create writer to file Write maximum and current member variables to file (each on own line) Loop through names array writing each to file

14 NameList Serializer public void read(String filename) { try { BufferedReader in = new BufferedReader( new FileReader(filename)); maximum = Integer.parseInt(in.readLine()); current = Integer.parseInt(in.readLine()); names = new String[maximum]; for (int i = 0; i < current; i++) { names[i] = in.readLine(); } Read in the maximum and current, parse them to integers and store in member variables Use that maximum to construct the array Use that current to know how many strings to read into names

15 Data Input/Output Streams Input: –DataInputStream : Reads data in binary form –FileInputStream used to connect to file Output: –DataOutputStream : Writes data in binary form –FileOutputStream used to connect to file Use overloaded version to append DataOutputStream out = new DataOutputStream( new FileOutputStream(filename), true)

16 Data Input/Output Streams Have different read/write methods for each simple type –Gets n next bits from file, where n is number of bits in that type –Converts to value for that type writeBoolean(b)readBoolean() writeChar(c)readChar(c) writeDouble(d)readDouble() writeFloat(f)readFloat() writeInt(i)readInt()… Get next 32 bits in file and interpret as an integer

17 Clock Example Must store inetger hour/minute in file Store as data file public void write(String filename) { try { DataOutputStream out = new DataOutputStream( new FileOutputStream(filename)); out.writeInt(hour); out.writeInt(minute); } … } hour (16 bits) minute (16 bits) Convert hour and minute to 32-bit binary and store in file

18 Data Input/Output Streams public void read(String filename) { try { DataInputStream in = new DataInputStream( new FileInputStream(filename)); hour = in.readInt(); minute = in.readInt(); } … } public Clock(String filename) {read(filename);} Get 32 bits from file and store as integer Can overload constructor to create a new object from a file


Download ppt "File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google