Java Programming: Reading from a File - dummies

Java Programming: Reading from a File

By Barry Burd

Many Java programs read characters from the computer’s keyboard. These programs use things like Scanner,, and nextDouble — things defined in Java’s API. The DoPayroll program in this listing puts a new spin on this story. Instead of reading characters from the keyboard, the program reads characters from the EmployeeInfo.txt file. The file lives on your computer’s hard drive.

import java.util.Scanner;
public class DoPayroll {
    public static void main(String args[])
                                  throws IOException {
        Scanner diskScanner =
            new Scanner(new File("EmployeeInfo.txt"));
        for (int empNum = 1; empNum <= 3; empNum++) {
    static void payOneEmployee(Scanner aScanner) {
        Employee anEmployee = new Employee();

To read characters from a file, you use some of the same things that help you read characters from the keyboard. You use Scanner, nextDouble, and other goodies. But in addition to these goodies, you have a few extra hurdles to jump. Here’s a list:

  • You need a new File object. To be more precise, you need a new instance of the API’s File class. You get this new instance with code like

    new File("EmployeeInfo.txt")

    The stuff in quotation marks is the name of a file — a file on your computer’s hard drive. The file contains characters.

    At this point, the terminology makes mountains out of molehills. Sure, the phrases new File object and new File instance appear, but all you’re doing is making new File(“EmployeeInfo.txt”) stand for a file on your hard drive. After you shove new File(“EmployeeInfo.txt”) into new Scanner,

    Scanner diskScanner =
              new Scanner(new File("EmployeeInfo.txt"));

    you can forget all about the new File business. From that point on in the code, diskScanner stands for the EmployeeInfo.txt filename on your computer’s hard drive. (The name diskScanner stands for a file on your hard drive just as, in previous examples, the name keyboard stands for those buttons that you press day-in and day-out.)

    Creating a new File object in the listing is like creating a new Employee object later in the same listing. The only difference is that the Employee and Account classes are defined in this book’s examples. The File class is defined in Java’s API.

    When you connect to a disk file with new Scanner, don’t forget the new File part. If you write new Scanner(“EmployeeInfo.txt”) without new File, the compiler won’t mind. (You won’t get any warnings or error messages before you run the code.) But when you run the code, you won’t get anything like the results that you expect to get.

  • You must refer to the File class by its full name — You can do this with an import declaration like the one in the listing. Alternatively, you can clutter up your code with a statement like

    Scanner diskScanner =
      new Scanner(new"EmployeeInfo.txt"));
  • You need a throws IOException clause. Lots of things can go wrong when your program connects to EmployeeInfo.txt. For one thing, your hard drive may not have a file named EmployeeInfo.txt.

  • For another, the file EmployeeInfo.txt may be in the wrong directory. To brace for this kind of calamity, the Java programming language takes certain precautions. The language insists that when a disk file is involved, you acknowledge the possible dangers of calling new Scanner.

    You can acknowledge the hazards in several possible ways, but the simplest way is to use a throws clause. In the listing, the main method’s header ends with the words throws IOException. By adding these two words, you appease the Java compiler.

    It’s as if you’re saying “I know that calling new Scanner can lead to problems. You don’t have to remind me.” And, sure enough, adding throws IOException to your main method keeps the compiler from complaining. (Without this throws clause, you get an unreported exception error message.)