有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中ByteBuffer的clear()方法

我在一个教程网站上读到了关于DatagramChannel的内容。他们解释了下面的示例程序

String newData = "New String to write to file..."
                    + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();

int bytesSent = channel.send(buf, new InetSocketAddress("example.com", 80));

他们增加了buf这一行。创建ByteBuffer类对象后清除()。clear()用于清除缓冲区。但一开始我们不需要澄清??。如果那里需要它,那么我们为什么要使用??。 谢谢(抱歉,如果有语法错误)


共 (0) 个答案