Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
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.

...