23 - Class File CS133 Developing Programming Principles...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Class File CS133: Developing Programming Principles Lecture 23 Binary Files, Serialization The class File is used to represent filenames in a system independent fashion: new File(String fileName); e.g., new File("C:\\hello.txt"); new File("/u/claclark/hi"); CS 133 Course Notes Lecture 23, Slide 2 1 2 Methods in Class File public public public public public boolean exists() boolean canRead() boolean canWrite() boolean delete() long length() Binary Files Binary files store data in the same format as the computer’s internal memory. Reading/writing binary files avoids expensive conversions to/from text formats. Lecture 23, Slide 3 CS 133 Course Notes Lecture 23, Slide 4 CS 133 Course Notes 3 4 Binary Files Example public class Coordinate { private int row, col; • Reading/Writing/Appending • Object Serialization public Coordinate(int row, int col) { this.row = row; this.col = col; } public int getRow() { return row; } public int getCol() { return col; } CS 133 Course Notes Lecture 23, Slide 5 CS 133 Course Notes Lecture 23, Slide 6 5 6 Example - Saving public void save(String fileName) throws IOException { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(fileName) ); out.writeInt(row); out.writeInt(col); out.close(); } CS 133 Course Notes Lecture 23, Slide 7 Example - Restoring public Coordinate(String fileName) throws IOException { ObjectInputStream in = new ObjectInputStream( new FileInputStream(fileName) ); row = in.readInt(); col = in.readInt(); in.close(); } CS 133 Course Notes Lecture 23, Slide 8 7 8 Example public static void main(String args) throws Exception { Coordinate x, y; String fileName = "C:\\test.dat"; x = new Coordinate(4, 5); x.save(fileName); y = new Coordinate(fileName); System.out.println(y.getRow()); System.out.println(y.getCol()); } } CS 133 Course Notes Lecture 23, Slide 9 Output to Binary Files ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(String fileName) ); May throw an IOException if the file can’t be created. CS 133 Course Notes Lecture 23, Slide 10 9 10 ObjectOutPutStream Methods: public void writeInt(int n) public void writeDouble(double x) public void writeBoolean(boolean b) ObjectOutPutStream public void flush() Forces an actual physical write of any buffered data to the file. public void close() Flushes the output stream and closes the file. All these methods throw IOException if an error occurs. (However, this is unusual.) CS 133 Course Notes Lecture 23, Slide 11 CS 133 Course Notes Lecture 23, Slide 12 11 12 Input from Binary Files ObjectInputStream in = new ObjectInputStream( new FileInputStream(String fileName) ); FileNotFoundException Thrown when a file of the specified name does not exist. It’s often worth handling IOException and FileNotFoundException in separate catch statements. CS 133 Course Notes Lecture 23, Slide 14 May throw FileNotFoundException or some other IOException. CS 133 Course Notes Lecture 23, Slide 13 13 14 ObjectInputStream Methods: public int readInt() public double readDouble() public boolean readBoolean() EOFException Thrown when an ObjectInputStream method tries to read beyond the end of the file. May be used with a catch statement to break out of an input loop. Lecture 23, Slide 15 CS 133 Course Notes Lecture 23, Slide 16 All these methods may throw an EOFException, or some other IOException, if an error occurs. CS 133 Course Notes 15 16 Example public static int intCount(String fileName) throws IOException { int n = 0; ObjectInputStream in = null; try { in = new ObjectInputStream( new FileInputStream(fileName)); while (true) { in.readInt(); n++; } } catch (EOFException e) { in.close(); } return n; } CS 133 Course Notes Lecture 23, Slide 17 Example try { String fileName = "C:\\test.dat"; ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(fileName)); out.writeInt(10); out.writeInt(11); out.writeInt(2005); out.close(); System.out.println(intCount(fileName)); } catch (IOException e) { } CS 133 Course Notes Lecture 23, Slide 18 17 18 Object I/O The writeObject method of the ObjectOutputStream class outputs objects to binary files. The readObject method of the ObjectInputStream class inputs objects from binary files. Object I/O To read or write objects, the class must be declared as serializable: import java.io.Serializable; public class Name implements Serializable { // Normal Java class } CS 133 Course Notes Lecture 23, Slide 19 CS 133 Course Notes Lecture 23, Slide 20 19 20 Example – Object I/O import java.io.*; public class Rectangle implements Serializable { private int height, width; public Rectangle(int height, int width) { this.height = height; this.width = width; } // etc. } CS 133 Course Notes Lecture 23, Slide 21 Example - Output Rectangle r0 = new Rectangle(2, 3); Rectangle r1 = new Rectangle(8, 10); String fileName = "C:\\test.dat"; try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream fileName) ); out.writeObject(r0); out.writeObject(r1); out.close(); } catch (IOException e) { } CS 133 Course Notes Lecture 23, Slide 22 21 22 Example - Input Rectangle r0, r1; String fileName = "C:\\test.dat"; try { ObjectInputStream in = new ObjectInputStream( new FileInputStream(fileName) ); r0 = (Rectangle)in.readObject(); r1 = (Rectangle)in.readObject(); in.close(); } catch (Exception e) { } CS 133 Course Notes Lecture 23, Slide 23 Summary • Class File • Binary files • Object I/O CS 133 Course Notes Lecture 23, Slide 24 23 24 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online