Java: Working with Disk Files

Some Java programs read characters from the computer’s keyboard. But the Java code in this listing reads characters from a specific file. The file in this example (named EmployeeInfo.txt) lives on the computer’s hard drive.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
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++) {
            payOneEmployee(diskScanner);
        }
        diskScanner.close();
    }
    static void payOneEmployee(Scanner aScanner) {
        Employee anEmployee = new Employee();
        anEmployee.setName(aScanner.nextLine());
        anEmployee.setJobTitle(aScanner.nextLine());
        anEmployee.cutCheck(aScanner.nextDouble());
        aScanner.nextLine();
    }
}

This EmployeeInfo.txt file is like a word processing document. The file can contain letters, digits, and other characters. But unlike a word processing document, the EmployeeInfo.txt file contains no formatting — no italics, no bold, no font sizes, nothing of that kind.

The EmployeeInfo.txt file contains only ordinary characters — the kinds of keystrokes that you type while you play a guessing game. Of course, getting guesses from a user’s keyboard and reading employee data from a disk file aren’t exactly the same.

In a guessing game, the program displays prompts, such as Enter an int from 1 to 10. The game program conducts a back-and-forth dialogue with the person sitting at the keyboard. In contrast, this listing has no dialogue. This DoPayroll program reads characters from a hard drive and doesn’t prompt or interact with anyone.

This code doesn’t run unless you have some employee data sitting in a file. The listing says that this file is EmployeeInfo.txt. So before running the code, a small EmployeeInfo.txt file needs to be created. The file is shown here.

image0.jpg

Refer to this figure for the resulting output.

image1.jpg

To keep the listing simple, when you type the characters in the figure, you finish up by typing 10000.00 and then pressing Enter. (Look again at the figure and notice how the cursor is at the start of a brand new line.) If you forget to finish by pressing Enter, then the code will crash when you try to run it.

Grouping separators vary from one country to another. The file shown in Figure 8-3 works on a computer configured in the United States where 5000.00 means "five thousand." But the file doesn't work on a computer that's configured in a "comma country" — a country where 5000,00 means "five thousand."

If you live in a comma country, and you use the file exactly as it's shown, you probably get an error message (an InputMismatchException) when you try to run this section's example. If so, change the number amounts in your file to match your country's number format. When you do, you should be okay.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com