
Java 9 中的 InputStream 类中添加了 transferTo() 方法。该方法已用于复制Java 中从输入流到输出流的数据。这意味着它从输入流中读取所有字节,并按照读取的顺序将字节写入输出流。
<strong>public long transferTo(OutputStream out) throws IOException</strong>
import java.util.Arrays;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class TransferToMethodTest {
public void testTransferTo() throws IOException {
byte[] inBytes = "tutorialspoint".<strong>getBytes()</strong>;
<strong>ByteArrayInputStream </strong>bis = new ByteArrayInputStream(inBytes);
<strong>ByteArrayOutputStream </strong>bos = new ByteArrayOutputStream();
try {
bis.<strong>transferTo</strong>(bos);
byte[] outBytes = bos.<strong>toByteArray</strong>();
System.out.println(<strong>Arrays.equals(</strong>inBytes, outBytes));
} finally {
try {
bis.close();
} catch(IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws Exception {
TransferToMethodTest test = new TransferToMethodTest();
test.testTransferTo();
}
}<strong>true</strong>
以上就是在Java 9中,transferTo()方法的重要性是什么?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号