Chapter 13: File Input and Output


In Chapter 12, you saw a few of the core Java packages and classes. You also learned that creating successful Java programs involves both writing your own code and using preexisting classes. This chapter will cover the fundamentals of reading and writing disk files. It will take advantage of several core classes in the java.io package. This will be your first look at making extensive use of core classes.

Files As Sequences of Bytes

In Chapter 1, you saw that a computer's memory is a clump of tiny circuits in which voltages represent 0s and 1s. It doesn't take a degree in electrical engineering to know that when you turn off a circuit's power, the voltages go away. No more 0s, no more 1s.

Disks are like computer memory in the following sense: A disk is a collection of tiny "somethings" that can be in one of two possible states. The surrounding electronics, and the software that controls the surrounding electronics, interpret the two states as representing 0 or 1. With a hard disk, the states are microscopic magnetic fields that can point in either of two directions. With a CD-ROM or DVD, the medium is filled with microscopic regions that either do or do not block light. Aside from the underlying physics, the main difference between disks and memory is that disks remember what is stored in them, even after the power goes off.

To make the rest of this discussion more clear, let's use the term RAM to mean ordinary computer memory, as distinct from disks, which are also a kind of memory. RAM is an acronym for Random Access Memory. It's a cool-sounding acronym, but you may be wondering what's so random about RAM. "Random" relates (distantly) to the amount of time it takes to read data out of memory or to write data into memory. It takes exactly the same amount of time (less than one millionth of a second) to read any byte in the circuit. Writing might take slightly longer than reading, but writing any byte takes exactly the same amount of time as writing any other byte. So you can pick any two bytes at random, and they can be read in the same amount of time, or written in the same amount of time, as each other.

Disks are not random access devices. At any moment, some parts of the disk data can be read more quickly than others. This is because the disk is rotating. If you want to read some data, you have to wait until it has rotated into position next to the disk's reading or writing hardware, which does not rotate. It you're lucky, the data will be just about rotated into position. If you're out of luck, the data will have just rotated out of position, and you will have to wait until the disk makes another revolution.

So you see that RAM and disks have very different mechanical and physical properties, but they both can be treated as storing ordered sequences of 0s and 1s.

As with RAM, you think of disks as being organized into bytes, each byte having a unique position. As with RAM, you would find it impossibly limiting if you had to think exclusively in terms of bytes. As with RAM, you use groups of disk bytes to encode higher-level multi-byte information. But unlike RAM, the first step in learning how to do disk input and output is to learn how to read and write pure bytes. That is where we will begin.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net