Files

Data stored in variables, arrays and objects are temporary. Data are lost when a local variable goes out of scope or when the program terminates.

For long-term retention of data, computers use files, which are stored on secondary storage devices such as hard disks and flash drives. Data maintained in files are persistent data because the data exist beyond the duration of program execution.

Java views each file as a stream, a sequence of bytes or characters. For example, we have used System.in (the standard input stream object) to input characters from the keyboard, and we have used System.out (the standard output stream object) to output character data to the screen.

The usual sequence of events for a file are to open a file for input (or output), to repeatedly read from (or write to) the file, and to close the file. When reading from a file, it is important to detect the end of file. It is also important to close any file or stream that your program opens. An open file consumes system resources, and not closing an output file might result in loss of data. In all of these operations, something might go wrong, so handling exceptions is an important part of processing files.

Java has many classes for performing input and output on files and streams (see the classes in the java.io package). Only a few will be mentioned here. All the example programs mentioned below are in chapter15.zip.

The Files class, Paths class, and Path interface provide information about files and directories. The FileAndDirectoryInfo illustrates the use of these classes.

Character-based input and output can be performed with the Scanner and Formatter classes. The Scanner class is commonly used to input data from the keyboard and can also be used to read data from a file. The Formatter class can be used to output to any text-based stream in a manner similar to System.out.printf. The CreateTextFile program illustrates the use of the Formatter class. Note that a try-catch is used to handle two of the exceptions that can occur when opening an output file. Use the following data when running the program.

100 Bob Blue 24.98
200 Steve Green -345.67
300 Pam White 0.00
400 Sam Red -42.16
500 Sue Yellow  224.62

The ReadTextFile program illustrates the use of the Scanner class to read data from a file. Again, note the use of try-catches to handle possible exceptions (one opening the file and the other for reading data from the file). Also, note that both CreateTextFile and ReadTextFile use the AccountRecord class to create objects from the data. Finally, note how the program determines the end-of-file.

The CreditInquiry program illustrates another example of reading from a text file. This program reads from the file and selectively prints data according the user's selection from a menu (the options are contained in MenuOption).

The final example this web page will mention is FileChooserDemo. This program illustrates the use of JFileChooser to let the user select a file.