Page 636 - Introduction to Programming with Java: A Problem Solving Approach
P. 636

                602 Chapter 15 Files
enter input from the keyboard, the input is not saved. A day later, if you want to use the same input, you have to enter it again.
For permanent or re-usable I/O, you can store input or output data in a file. A file is a group of related data that is typically stored in a contiguous block on a non-volatile storage device (such as a hard disk). Data files are fundamentally the same as the .java and .class program files that you have been using all along to hold your java programs. But instead of holding programs, data files hold data that programs read from for input or write to for output. To keep from confusing yourself or your computer, for your data files, you should name them with an extension that identifies them as data, like .txt or .data.
In this chapter you’ll learn how to write code that creates output data files and stores data in those files, and you’ll learn how to write code that reads data from pre-existing input files. You’ll learn how to do these things with simple text files, which hold their data as text characters. You’ll learn how to do these things more efficiently with binary files, which hold data in native format—the format your computer uses when it processes data during program execution. And you’ll learn how to store complete objects in object files.
15.2 Java API Classes You Need to Import
In programs that manipulate files, you will use several of Java’s pre-built classes. To access these classes, you need to import the packages that contain them. Figure 15.1 shows the subset of Java API classes we’ll discuss in this chapter. You’ll recognize the Scanner class because you’ve been using it to get input from a keyboard. In this chapter, you’ll learn how to use it to get input from a file, too. As you know, you can import it with this statement:
            Apago PDF Enhancer
All the rest of the classes in Figure 15.1 are in the java.io package. Usually, you’ll need more than one of them, and you can import any combination with this wildcard statement:
import java.io.*;
The three groupings that appear in Figure 15.1, that is, “text,” “binary,” and “object,” identify three dif- ferent I/O strategies. Each has its place. Text I/O is a handy way to store primitive data types. It’s relatively easy to read or write text files in Java, and you can use almost any other kind of computer program (like word processors and spreadsheets) to read or write text files. Binary files hold data more efficiently than either text files or object files. When the data to be stored is complex—involves objects or a combination of objects and primitives—object files are easiest to use. Be aware that it’s hard to inspect a binary file or object file because you can’t read such files with text processing programs. To read a binary file, you must know how it’s organized. To read an object file, you must know the types of objects it holds, but you can ignore their internal structure because structural information inserted by the JVM when you write an object file tells the JVM how the file is organized when you read it.
In addition to the classes that appear in Figure 15.1, there are also many other classes that deal with file I/O, and their operations overlap. In other words, some classes handle the same sort of file operation as other classes do. Why are there so many classes? Each class has different features, so certain classes work bet- ter in certain situations. There’s also an historical reason. File I/O classes were added to the Java language incrementally. Whenever the Java designers realized that the file I/O classes were deficient in some way, they didn’t want to modify the existing classes because that would mess up existing code that depended on those classes. So they added new classes. Unfortunately, the result is that there are many file I/O classes, and it’s hard to remember what they all do. We’ll discuss only those that are the most useful and most straightforward.
 import java.util.Scanner;























































































   634   635   636   637   638