提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
填充和排空缓冲区:一次一个元素。
import java.nio.CharBuffer; public class Main{ public static void main(String[] argv) throws Exception {/**来 自 nowjava.com - 时 代 Java**/ CharBuffer buffer = CharBuffer.allocate(100); while (fillBuffer(buffer)) { buffer.flip(); drainBuffer(buffer); buffer.clear(); } } private static void drainBuffer(CharBuffer buffer) { while (buffer.hasRemaining()) { System.out.print(buffer.get()); } System.out.println(""); } /** from 时代Java公众号**/ private static boolean fillBuffer(CharBuffer buffer) { if (index >= strings.length) { return (false); } String string = strings[index++]; for (int i = 0; i < string.length(); i++) { buffer.put(string.charAt(i)); } return (true); } private static int index = 0;