Java Reference
In-Depth Information
NOTE If you have not come across Fibonacci numbers before, they are not as dull
and uninteresting as they might seem. Fibonacci numbers relate to the way many
plants grow their leaves and petals and the way spiral seashells grow,. They are
even behind the relationship between the height and width of classical buildings.
TIP Don't be too hasty deleting this or other files that you create later in this
to read files.
How It Works
The first three statements in main() set up the variables you need to generate the Fibonacci numbers.
The first two Fibonacci numbers are 0 and 1. All subsequent numbers are the sum of the preceding two
numbers. The program stores the last two numbers in the fiboNumbers array. The index variable is used
to access this array to store the most recent number.
As the numbers are generated, they are stored in a buffer before they are written to the file. The
ByteBuffer object, buf , is created with a capacity to store count numbers. We create a view buffer,
longBuf , from buf to make it easy to store the numbers directly in the buffer. Type long is appropriate
here because Fibonacci numbers grow quite rapidly. I'll be explaining buffers in more detail later in this
chapter, but for the moment, a view buffer allows you to treat a buffer that contains bytes as a sequence
of values of some other type, type long in this case. Ultimately you have to write a sequence of long
values (or values of any other type) to a file as bytes, so this enables you set up the long values in a
buffer, then treat them as bytes when you need to write them to the file.
The file object specifies an absolute path to the fibonacci.bin file in the "Beginning Java Stuff"
directory in your home directory. Your home directory is specified by the system property value corres-
ponding to the key "user.home" . The try block that follows the creation of the path object is there to
ensure that the directory that is to contain the file exists, or will be created if it doesn't. The createDir-
ectories() method creates a directory together with all directories in the path when necessary; if the
directory already exists, it does nothing.
The try block with resources creates the BufferedOutputStream object for the file between the par-
entheses so it is flushed and closed automatically. Because no options are specified for the newOut-
putStream() method, the file is created when necessary, and the file is always written from the begin-
ning, overwriting any existing contents. This implies that if you run the example more than once, you
still end up with the same file contents.
The Fibonacci numbers are created in the loop. As each number is created, it is stored in the fiboNum-
bers array and then written to longBuf , which is an overlay of buf . The put() method automatically
increments the current position in the buffer, ready for the next value to be written. The value of index is
incremented modulo 2, so it flips between 0 and 1. This has the effect that each number that is generated
is stored in the array always replacing the oldest value of the two.
Search WWH ::

Custom Search