...
| Code Block | ||
|---|---|---|
| ||
ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
try(FileChannel rdr = (new FileInputStream("file")).getChannel()){
while (rdr.read(buffer) > 0) {
// Do something with the buffer
buffer.clear();
}
}
catch (Exception e) {
System.out.println("Exception creating file channel" + e);//handle error
}
|
Note that manual clearing of the buffer data is mandatory because direct buffers are exempt from garbage collection.
...