Java Reference
In-Depth Information
sb.append("And all the while my eyes I kept");
sb.append(lineSeparator);
sb.append("On the descending moon.");
return sb.toString();
}
}
Data has been written to C:\book\javabook\luci5.txt
A file has two kinds of data associated with it. One is its contents and the other is metadata such as creation time,
last-modified time, etc. When you write data to a file channel, the data may not be actually written to the storage
device (for example, the hard disk) immediately. To write the data to the storage device immediately, after a call to
the write() method on a file channel, you can call its force(boolean metaData) method. It guarantees that the file's
contents and metadata are written to its storage device. If you call force(false) , only the file's metadata is written to
the storage device. If you call force(true) , both the file's content and its metadata are written to the storage device.
In fact, this is guaranteed only if the storage device is local. Otherwise, the JVM tries its best to write the data to the
storage device.
a file channel works only with byte buffers. In the examples in this section, I have assumed that a character is
represented in a byte, which is true only when you are using an encoding such as uS-aSCII or utF-8 for english alphabets.
please refer to the “Character Set” section on how to encode a character buffer into a byte buffer and how to decode a
byte buffer into a character buffer.
Tip
Memory-Mapped File I/O
There is another way to perform I/O on a file, which is by mapping a region of the file into physical memory and
treating it as a memory array. This is the fastest way available to perform file I/O in Java. Using a special kind of byte
buffer called MappedByteBuffer lets you perform memory-mapped file I/O.
For memory-mapped file I/O, start by obtaining a FileChannel object for the file, and use the map() method of
the FileChannel to get a MappedByteBuffer . Read or write directly to the mapped byte buffer instead of using the
read() or write() method of the FileChannel object. When you read from the mapped byte buffer, you read from the
file's region you have mapped. When you write to the mapped byte buffer, you write to the mapped region of the file.
If you want to write the written data to the mapped byte buffer immediately to the storage device, you need to use the
force() method of the mapped byte buffer. There is no boolean argument to force() related to metadata.
Once you obtain the mapped byte buffer from a FileChannel , closing the channel has no effect on your buffer.
You can keep reading/writing the mapped byte buffer, even after the FileChannel is closed.
You can map a region of a file in a read-only, read-write, or private mode. In a read-only mode, you can only read
from the mapped byte buffer. In a read-write mode, you can read from as well as write to the mapped byte buffer. The
private mode needs a little explanation. This mode is also called a copy-on-write mode. When multiple programs map
the same region of a file, a separate copy of that region is not created for each program. Rather, all programs share
the same region of the file. However, when a program modifies the mapped region, a separate copy of that region is
created only for that program, which is its private copy. Any modification made to the private copy is not visible to
other programs.
 
 
Search WWH ::




Custom Search