Java Reference
In-Depth Information
Since we are using a relative put operation, loading the view buffer will change the position for that
buffer to reflect the number of values transferred to it. However, the backing byte buffer that we use in
the channel write operation will still have its limit and position unchanged. We therefore set the limit
for the byte buffer with the statement:
buf.limit(8 * longBuf.position());
Since each prime occupies 8 bytes, multiplying the position value for the view buffer by 8 gives us the
number of bytes occupied in the primary buffer. We then go ahead and write that buffer to the file and
increment primesWritten by the position value for the view buffer, since this will be the number of
primes that were written. Before the next iteration we call clear() for both buffers to reset their limits
and positions to their original states - 0 and the capacity respectively. When we have written all the
primes, the loop will end and we output the length of the file before closing it.
Since this file contains binary data, we will not want to view it except perhaps for debugging purposes.
Writing Mixed Data to a File
Sometimes, you may want to write more than one kind of data to a file. You may want to mix integers
with floating-point values with text perhaps. One way to do this is to use multiple view buffers. We can
illustrate the principle of how this works by outputting some text along with each binary prime value in
the previous example. Rather than taking the easy route by just writing the same text for each prime
value, let's add a character representation of the prime preceding each binary value. We'll add
something like prime = xxx ahead of each binary value. The first point to keep in mind is that if we
ever want to read the file successfully, we can't just dump strings of varying lengths in it. You would
have no way to tell where the text ended and where the binary data began. We either have to fix the
length of the string or provide data in the file that specifies the length of the string. We will therefore
choose to write the data corresponding to each prime as three successive data items:
1.
A count of the length of the string as binary value (it would sensibly be an integer type but
we'll make it type double since we need the practice)
2.
The string representation of the prime value: prime = xxx
3.
The prime as a binary value of type long
The basic prime calculation will not change at all, so we only need to update the shaded code at the end
in the previous example that writes the file.
The basic strategy we will adopt is to create a byte buffer, and then create a series of view buffers that
map the three different kinds of data into it. A simple approach would be to write the data for one
prime at a time, so let's try that first. Setting up the file stream and the channel will be more or less
exactly the same:
File aFile = new File("C:/Beg Java Stuff/primes.txt");
FileOutputStream outputFile = null;
try {
outputFile = new FileOutputStream(aFile);
Search WWH ::




Custom Search