I've been getting some strange outputs from this code upon reading from a large file, the file was printed using a while loop to 99,999 digits however, upon reading the file and printing the contents it only outputs 99,988 lines. Also, is using a ByteBuffer the only option for reading back the file? I've seen some other code using a CharBuffer, but I'm not sure which one I should use, and in what cases I should use them. NOTE: filePath is a Path object pointing to a file on the disk.
private void byteChannelTrial() throws Exception {
try (FileChannel channel = (FileChannel) Files.newByteChannel(filePath, READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
String encoding = System.getProperty("file.encoding");
while (channel.read(buffer) != -1) {
buffer.rewind();
System.out.print(Charset.forName(encoding).decode(buffer));
buffer.clear();
}
}