Java Input & Output

Streams

  • System.in is an input stream that connects to the keyboard
  • System.out is an output stream that connects to the screen
  • PrintWriter is a stream class that can be used to write to a text file
    • All the file I/O classes that follow are in the package java.io, so a program that uses PrintWriter will start with a set of import statements:
      • import java.io.PrintWriter;
      • import java.io.FileOutputStream;
      • import java.io.FileNotFoundException;
PrintWriter outputStreamName;
outputStreamName = new PrintWriter(new
                    FileOutputStream(FileName));

// The class FileOutputStream takes a string representing the file name as its argument
// The class PrintWriter takes the anonymous FileOutputStream object as its argument

  • When a program is finished writing to a file, it should always close the stream connected to that file
    • outputStreamName.close();

IOException

  • When performing file I/O there are many situations in which an exception, such as FileNotFoundException, may be thrown
  • Many of these exception classes are subclasses of the class IOException
    • The class IOException is the root class for a variety of exception classes having to do with input and/or output
  • These exception classes are all checked exceptions
    • Therefore, they must be caught or declared in a throws clause

Unchecked Exceptions

  • In contrast, the exception classes NoSuchElementException, InputMismatchException, and IllegalStateException are all unchecked exceptions
    • Unchecked exceptions are not required to be caught or declared in a throws clause

Appending to a Text File

  • To create a PrintWriter object and connect it to a text file for appending, a second argument, set to true, must be used in the constructor for the FileOutputStream object
    • outputStreamName = new PrintWriter(new FileOutputStream(FileName, true));

Reading From a Text File Using Scanner

  • The class Scanner can be used for reading from the keyboard as well as reading from a text file
    • Simply replace the argument System.in (to the Scanner constructor) with a suitable stream that is connected to the text file
      • Scanner StreamObject =┬ánew Scanner(new FileInputStream(FileName));
  • Methods of the Scanner class for reading input behave the same whether reading from the keyboard or reading from a text file
    • For example, the nextInt and nextLine methods

Input/Output Syntax

Import Statements

  • import java.util.Scanner;
  • import java.io.File;
  • import java.io.PrintWriter;
  • import java.io.FileOutputStream;
  • import java.io.FileNotFoundException;

Creating a file

  • File fileObject = new File(fileName);

Output Stream

         PrintWriter outputStream = null;
         try
         {
             outputStream = 
                  new PrintWriter(new FileOutputStream(fileName));
         }
         catch(FileNotFoundException e)
         {
             System.out.println("Error opening the file " + fileName);
             System.exit(0);
         }

Syntax