Java Reference
In-Depth Information
Chapter 6
File Processing
Introduction
6.1 File-Reading Basics
Data, Data Everywhere
In Chapter 3 we discussed how to construct a Scanner object to read
input from the console. Now we will look at how to construct Scanner
objects to read input from files.The idea is fairly straightforward, but Java
does not make it easy to read from input files.This is unfortunate because
many interesting problems can be formulated as file-processing tasks.
Many introductory computer science classes have abandoned file process-
ing altogether and left the topic for the second course because it is con-
sidered too advanced for novices.
Files and File Objects
Reading a File with a Scanner
6.2 Details of Token-Based
Processing
Structure of Files and
Consuming Input
Scanner Parameters
Paths and Directories
A More Complex Input File
6.3 Line-Based Processing
There is nothing intrinsically complex about file processing, but Java was
not designed for it and Sun has not been particularly eager to provide a
simple solution. Sun did, however, introduce the Scanner class as a way
to simplify some of the details associated with reading files.The result is
that file reading is still awkward in Java, but at least the level of detail is
manageable.
String Scanner s and
Line/Token Combinations
6.4 Advanced File Processing
Output Files with
PrintStream
6.5 Case Study: Zip Code
Lookup
Before we start writing file-processing programs, we have to explore
some issues related to Java exceptions. Remember that exceptions are
errors that halt the execution of a program. In the case of file processing,
trying to open a file that doesn't exist or trying to read beyond the end of
a file generates an exception.
373
 
Search WWH ::




Custom Search