I am using following code to read a sub-sequence from a file.
FileChannel channel = new RandomAccessFile("abc.txt", "r").getChannel();
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
CharBuffer cbuf = buffer.asCharBuffer();
String str = cbuf.subSequence(0, 1).toString();
System.out.println("str = " + str);
However, this gives output “str = ?” Can anybody help me why is this happening and how to solve this ? Sorry, I am new in NIO.
Additional thing, when I add System.out.println(buffer); it gives output “java.nio.DirectByteBufferR[pos=0 lim=16 cap=16]”.
System.out.println((char)buffer.get(0)) gives proper output means 1st character.
Try with this method, you have an encoding issue :