You will need to flip() buffer before you can read from the buffer. Before reading data from the buffer, you need to call the flip() method. When the flip() method is called the limit, the current position is set, and the position is 0. For example,
CharBuffer charBuf = CharBuffer.allocate(1000); for (int i = 0; i < 10; i++) { String text = "testing" + i + "\n"; charBuf.put(text); } charBuf.flip(); System.out.println(charBuf);
The above will only print characters in buffers, not in unwritten space in the buffer.
source share