File I/O Setup

If we want our program to read from a text file or write to a text file, we need to add two import statements:

import java.io.*;
import java.util.*;

We will also need to add the clause throws IOException to the end of the main method (or, when we start writing multiple methods, to the end of whatever method wants to work with a text file). The main method should instead look like this:

public static void main(String[] args) throws IOException
{

}

(We can similarly put throws IOException at the end of a different method.) The throws IOException clause means that interacting with files can cause some unexpected problems (like if the file isn’t there), but that we’re not going to handle them. Later in the course we will discuss how to handle these problems.