Files and Streams


A stream is the term used by Java to refer to the transfer of data from one point to the next. The data is transferred as a stream of bytes.

For this lecture, we will focus on reading and writing file streams. While there are several different types of file streams to choose from, we will stick with the simplest to understand: reading and writing lines of text.

Before we can use the streams, we have to import the package:

		import java.io.*;
Let's start by opening a file for output. We'll use the FileWriter class. One thing to note about all methods of the java.io classes is that they throw an IOException, which must be caught.
		try {
			FileWriter fw = new FileWriter(fileName);
		}
		catch (IOException ioe) {}
The FileWriter class has a constructor that takes the name of a file as an argument. We could stick with this one. The problem with it, however, is that it doesn't buffer it's write process- bytes are written immediately. What we'll do is wrap our FileWriter in a BufferedWriter.
		try {
			FileWriter fw = new FileWriter(fileName);
			BufferedWriter bw = new BufferedWriter(fw);
		}
		catch (IOException ioe) {}
The BufferedWriter class has a constructor that takes a FileWriter as an argument. Now we're ready to write text to the file.
		try {
			FileWriter fw = new FileWriter(fileName);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(lineOfText);
		}
		catch (IOException ioe) {}
When we're finished writing, we need to flush and close the file stream.
		try {
			FileWriter fw = new FileWriter(fileName);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(lineOfText);
			bw.flush();
			bw.close();
		}
		catch (IOException ioe) {}
Now, let's open a file for input. We'll use the FileReader class. In addition to IOException, we need to catch FileNotFoundException. Notice that it's placed BEFORE the IOException. The reason for this is that FileNotFoundException extends (is a sub-class of) IOException. Therefore, if the file is not found, it would be caught by IOException, not FileNotFoundException, if that exception was listed first.
		try {
			FileReader fr = new FileReader(fileName);
		}
		catch (FileNotFoundException fnfe) {}
		catch (IOException ioe) {}
The FileReader class has a constructor that takes the name of a file as an argument. We could stick with this one. The problem with it, however, is that it doesn't buffer it's read process- bytes are read immediately. What we'll do is wrap our FileReader in a BufferedReader.
		try {
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
		}
		catch (FileNotFoundException fnfe) {}
		catch (IOException ioe) {}
The BufferedReader class has a constructor that takes a FileReader as an argument. Now we're ready to read text from the file.
		try {
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			String input = br.readLine();
		}
		catch (FileNotFoundException fnfe) {}
		catch (IOException ioe) {}
The readLine() method returns a null when the EOF is reached. So, we could write a while loop that reads until a null is returned.
		try {
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			String input = br.readLine();
			while ( input != null ) {
				System.out.println(input);
				input = br.readLine();
			}
		}
		catch (FileNotFoundException fnfe) {}
		catch (IOException ioe) {}