Java Reference
In-Depth Information
Chapter 4
File Handling
Learning Objectives
After reading this chapter, you should:
￿ know how to create and process serial fi les in Java;
￿ know how to create and process random access fi les in Java;
￿ know how to redirect console input and output to disc fi les;
￿ know how to construct GUI-based fi le-handling programs;
￿ know how to use command line parameters with Java programs;
￿ understand the concept and importance of Java's serialisation mechanism and
know how to implement it;
know how to make use of ArrayLists for convenient packaging of serialised objects.
With all our programs so far, there has been a very fundamental limitation: all data
accepted is held only for as long as the program remains active. As soon as the
program fi nishes execution, any data that has been entered and the results of
processing such data are thrown away. Of course, for very many real-life applica-
tions (banking, stock control, fi nancial accounting, etc.), this limitation is simply
not realistic. These applications demand persistent data storage. That is to say, data
must be maintained in a permanent state, such that it is available for subsequent
further processing. The most common way of providing such persistent storage is to
use disc fi les. Java provides such a facility, with the access to such fi les being either
serial or random . The following sections explain the use of these two fi le access
methods, fi rstly for non-GUI applications and later for GUI applications. In addition,
the important and often neglected topic of serialisation is covered.
Serial Access Files
Serial access fi les are fi les in which data is stored in physically adjacent locations,
often in no particular logical order, with each new item of data being added to the
end of the fi le.
Search WWH ::

Custom Search