Java Reading from a File using FileChannel -
i've been getting strange outputs code upon reading large file, file printed using while loop 99,999 digits however, upon reading file , printing contents outputs 99,988 lines. also, using bytebuffer option reading file? i've seen other code using charbuffer, i'm not sure 1 should use, , in cases should use them. note: filepath path object pointing file on 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(); } }
usually, flip() called before buffer data read. rewind() method bellowing works:
public final buffer rewind() { position = 0; mark = -1; return this; }
it not set 'limit' flip() does:
public final buffer flip() { limit = position; position = 0; mark = -1; return this; }
so, take tray using flip() instead of rewind() before reading.
Comments
Post a Comment