ICS4U Grade 12 – Computer Science – Files and Handing Exceptions

Grade 12 – Computer Science

Files and Exceptions Handling – A Guide to Programming in Java Ch 12


What is a File?

  • Files: are data stored into a computer’s memory that remains persistent. These files will not disappear when the computer is powered off.


The File Class

  • The file class, part of java.io package creates object that represents a file
  • It can create a new file, test existence of a file, or delete a file
    • File(String f) – creates a new File object and refers to it as file f
    • createNewFile() – creates a new file with filename specified in constructor (IOException and return false if file cannot be created, true of created)
    • delete() – deletes that file, returns true of successful
    • exists() – returns true of a file exists
  • For paths in a filename, escape sequence (\\) should be used to show backslashes


Handling Exceptions

  • Exceptions are errors affecting program execution
  • If they are not handled, then the program will terminate
  • Although usually the program will end anyways, you can greet the user with a better error message.
  • try – catch – finally statement can be used to write an exception handler
    • try contains statements which might generate an exception
    • catch will execute statements if the exception in the parameter occurred
      • Separate catch clauses should be used for each type of exception
    • finally is optional and will execute regardless of what happens in the try-catch clauses
  • IOException throw: certain methods can throw exceptions when there is an error
    • createNewFile() throws an IOException exception if it cannot create a new file
  • An exception is an object of the Throwable class
    • These objects have getMessage() member that returns string containing information about the error
    • err stream is used to display error messages


The File Streams

  • File Stream keeps track of file position when the stream processes charactors
  • Sequential File Access is performed to read one character after another or 1 line after another
  • A stream has a sequence of characters:
    • Cr followed by Lf means its a line terminator
    • -1 is the end of file


FileReader and BufferedReader Classes

  • FileReader create the input file stream.
  • BufferedReader is used to read text from the stream
  • FileReader Class
    • FileReader(File fileName) – creates file stream for File object, throws FileNotFoundException if file does not exist
    • close() – closes the input file stream. Throws IOException if it cannot be closed
  • BufferedReader Class
    • BufferedReader(Reader stream) – creates a buffered-input stream from stream. Reader is a FileReader superclass.
    • read() – reads a single character from the input stream. This method throws an IOException exception if stream cannot be read
    • readLine() – reads a line of text from the input stream. This method throws IOException exception if it cannot be read.
    • close() – closes the input file stream. Throws IOException..


Processing Numeric Data

  • Files on a disk are a set of characters, even if they are numeric data.
  • Double Class (java.lang.Double)
    • parseDouble(String text) – returns the double value in the String text
  • Integer Class (java.lang.Integer)
    • parseInteger(String text) – returns the integer value in the String text


FileWriter and BufferedWriter Classes

  • FileWriter and BufferedWriter classes are used together to write to a file
  • FileWriter creates an out stream
  • BufferedWriter sends the text to the stream
  • FileWriter class 
    • FileWriter(File fileName, boolean append) – creates output file stream for File object. If append is true, it will continue writing to the existing file. Otherwise, it will be overwritten. Constructor throws IOException. New file will be created if no File exists
    • close() – self explanatory
  • BufferedWriter class
    • BufferedWriter(Writer stream) – buffered writer stream from stream writer
    • newLine() – writes new line character to output stream. IOException if ..
    • write(String str) – writes str to the output stream, method throws IOException..
    • write(char c) – writes c to output stream, method throws IOException..
    • close() – self explanatory


Object Serialization

  • Object Serialization: writing objects to a file
  • The class information about an object is written out to a stream
    • If a class uses another class, this information is also written out
  • Object Deserialization: when objects are retrieved from an object
  • FileOutputStream and ObjectOutputStream are used to write objects to files
  • FileInputStream and ObjectInputStream are used to read objects from files
  • FileOutputStream
    • FileOutputStream(File fileName, boolean append) – creates an output file stream. Same rules apply to FileReader.
    • close() – self explanatory
  • ObjectOutputStream
    • ObjectOutputStream(FileOutputStream stream) – creates object stream from stream
    • writeObject(Object obj) – writes information to the output stream
    • writeInt(int num) – writes num to the file stream
    • writeDouble(double num) – writes a double to the output stream
    • close()
  • FileInputStream
    • FIleInputStream(File fileName) – creates input stream for FIle object
    • close()
  • ObjectInputStream
    • ObjectInputStream(FileInputStream stream) – creates an object stream from stream. Throws IOException exception if stream cannot be read
    • readObject() reads an object from the input stream. Throws a ClassNotFoundException if the stream cannot be read or cannot be deserialized
    • readInt() reads an int from the input stream.
    • readDouble() read a double from the input stream
    • close() 
  • Serializable Interface: classes that make objects waiting to be written to objects need
  • to implements the Serializable interface
    • The interface needs no implementation, just that it needs it to write it
  • Objects read from readObject() requires casting to the appropriate type