Java Reference
In-Depth Information
You might use this version of the write() method in a sequence of writes to update a particular part of
the file without disrupting the primary sequence of write operations. For example, you might record a count
of the number of records in a file at the beginning. As you add new records to the file, you could update the
count at the beginning of the file without changing the file position recorded by the channel, which would
be pointing to the end of the file where new data is to be written.
A FileChannel object has two versions of the position() method. The position() method without a
parameter returns the file position as type long . It is of type long to provide for large files that could con-
tain more than two billion bytes. You can also set the file position by calling the position() method with
an argument of type long that specifies a new position. This sets the file's position to the new value. For
example, if you have a reference to a file channel stored in a variable outputChannel , you could alter the
file position with the following statements:
try {
outputChannel.position(fileChannel.position() - 100L);
} catch (IOException e) {
e.printStackTrace();
}
This moves the current file position back by 100 bytes. This could be because you have written 100 bytes
to the file and want to reset the position so you can rewrite it. The call to the position() method should
normally be in a try block because it can throw an exception of type IOException if an I/O error occurs.
You can set the file position beyond the end of the file. If you then write to the file, the bytes between the
previous end of the file and the new position contain junk values. If you try to read from a position beyond
the end of the file, an end-of-file condition is returned immediately.
Using a View Buffer to Load a Byte Buffer
The code in the previous example is not the only way of writing the string to the buffer. You could have used
a view buffer, like this:
ByteBuffer buf = ByteBuffer.allocate(1024);
CharBuffer charBuf = buf.asCharBuffer();
charBuf.put(phrase); // Transfer string to buffer
buf.limit(2*charBuf.position()); // Update the byte buffer limit
You can then write the contents of buf to the channel as in the example.
Transferring the string via a view buffer of type CharBuffer is much simpler. The only fly in the oint-
ment is that the backing ByteBuffer has no knowledge of any data you put in the view buffer. The position
for buf is still sitting firmly at zero with the limit set as the capacity, so flipping it won't set it up ready to
write to the channel. However, all you have to do is set the limit for buf to correspond to the number of
bytes that you transferred to the view buffer.
Of course, if you were writing the file for use by some other program, writing Unicode characters could
be very inconvenient if the other program environment did not understand it. In this case you would write
the data as bytes in the local character encoding, as you did in the first example in this chapter.
Writing Varying Length Strings to a File
Search WWH ::




Custom Search