Java IO Classes



Introduction

Perhaps Java Input and Output (I/O) doesn't make as much logical sense as it should. Much of the following information will just have to be memorized, though we've tried to organize it as much as possible. The best way to learn this material is through practice.

In general, reading input is done at the BufferedReader level, since it has a convenient readLine() method. Writing output is done at the Printers level, since Printer objects have the inverse methods print() and println().

When we call the readLine() method, we will always get back the entire line of that source (file or command line) as a String object. This makes it difficult to distinguish between numbers and words. Fortunately, we have the StringTokenizer class as well as the class methods Integer.parseInt(String s) and Double.parseDouble(String s) which can help us to tackle these issues.


Suggested Input/Output Formulas

OutputGoal: To Obtain a Printer

Console Output
  1. System.out is a PrintStream object.
  2. You can either use it as is, or create a PrintWriter object taking System.out in the constructor.
PrintWriter stdout = new PrintWriter(System.in);

File Output
  1. Create a new File object passing into the constructor the file's name as a String.
  2. Create a new FileWriter object passing in the File object to the constructor.
  3. Create a new PrintWriter object passing in the FileWriter object to the constructor.
File file = new File("filename");
FileWriter writer = new FileWriter(file);
PrintWriter fileout = new PrintWriter(writer);

####Input ###Goal: To Obtain a BufferedReader

Console Input
  1. System.in is an InputStream object.
  2. Create a new InputStreamReader object passing in System.in to the constructor.
  3. Create a new BufferedReader object passing in the InputStreamReader object to the constructor.
InputStreamReader reader = new InputStreamReader(System.in);
BufferdReader stdin = new BufferedReader(reader);

File Input
  1. Create a new File object pasing into the constructor the file's name as a String.
  2. try to create a new FileReader object by passing in the File object to the constructor.
  3. Create a BufferedReader object passing in the FileReader object to the constructor.
  4. catch the FileNotFoundException that creating a new FileReader object generated.
File file = new File("filename");
try {
FileReader reader = new FileReader(file);

BufferedReader filein = new BufferedReader(reader);

catch (FileNotFoundException fnfe) {
stdout.println("ERROR: File " + filename + " not found!");

// handle exception by reprompting or quiting

}




Class Summary
  Readers Input
Source
Output
Destination
Writers
Printers     PrintStream PrintWriter
Buffers Buffered Reader     BufferedWriter
  Reader     Writer
Console InputStreamReader InputStream OutputStream OutputStreamWriter
Files FileReader File FileWriter





Relational Diagram

In the following diagram, two types of relations are identified. The first is inheritance, which we know already. The second is a form of constructor dependency. Keep in mind that because of inheritance, a BufferedReader may take in any Reader object to its constructor, including FileReaders and InputStreamReaders.

Keep in mind that, ultimately, data is taken from or written to the File or Stream objects. But we do this at the BufferedReader and Printer levels. Make sure you understand how these links are occurring.





Class Commonly Used Methods and Constructors
Return Type Method Name Parameter List Throws Clause
Reader
  Reader ()  
void close () throws IOException
boolean ready () throws IOException
void reset () throws IOException
Writer
  Writer ()  
void close () throws IOException
void flush () throws IOException
BufferedReader extends Reader
  BufferedReader (Reader r)  
String readLine () throws IOException
BufferedWriter extends Writer
  BufferedWriter (Writer w)  
void newLine () throws IOException
PrintWriter extends Writer
  PrintWriter (OutputStream)  
  PrintWriter (Writer)  
void print (String s)  
void println (String s)  
PrintStream extends OutputStream
  PrintStream (OutputStream os)  
void print ()  
void println ()  
InputStream
  InputStream ()  
OutputStream
  OutputStream ()  
InputStreamReader extends Reader
  InputStreamReader (InputStream is)  
OutputStreamWriter extends Writer
  OutputStreamWriter (OutputStream os)  
File
  File (String filename)  
boolean exists ()  
boolean canRead ()  
boolean canWrite ()  
boolean isFile ()  
boolean isDirectory ()  
FileReader extends InputStreamReader
  FileReader (File file) throws FileNotFoundException
FileWriter extends OutputStreamWriter
  FileWriter (File file)  
StringTokenizer
  StringTokenizer (String line, String delimiter)  
int countTokens ()  
boolean hasMoreTokens ()  
String nextToken () throws NoSuchElementException
Integer extends Number
String parseInt (String number) throws NumberFormatException