Java Reference
In-Depth Information
for (int i = 0; i < size && i < Integer.MAX_VALUE; i++)
if (cb.charAt(i) == ch)
count++;
fc.close();
return count;
}
We use a FileInputStream to get a channel for the file. Then we create
a mapped buffer for the entire file. What a "mapped buffer" does may
vary with the platform, but for large files (greater than a few tens of
kilobytes) you can assume that it will be at least as efficient as stream-
ing through the data, and nearly certainly much more efficient. We then
get a decoder for the specified character set, which gives us a CharBuffer
from which to read. [4]
[4] Note that there is an unfortunate discrepancy between the ability to map huge files and the fact that
the returned buffer has a capacity that is limited to Integer.MAX_VALUE.
The CharBuffer not only lets you read (decoded) characters from the file,
it also acts as a CharSequence and, therefore, can be used with the regu-
lar expression mechanism.
In addition to high-performance I/O, the new I/O package also provides
a different programming model that allows for non-blocking I/O opera-
tions to be performed. This is an advanced topic well beyond the scope
of this topic, but suffice it to say that this allows a small number of
threads to efficiently manage a large number of simultaneous I/O con-
nections.
There is also a reliable file locking mechanism: You can lock a FileChan-
nel and receive a java.nio.channels.FileLock object that represents
either a shared or exclusive lock on a file. You can release the FileLock
when you are done with it.
Nothing has really happened until it has been recorded.
Virginia Woolf
 
 
Search WWH ::




Custom Search