[英]What is java.io.Reader transferTo(java.io.Writer) method in Java 10 ? Is there a similar method for InputStream?
I read the Java 10 documentation on java.io.Reader.transferTo(...)
and it says: 我在java.io.Reader.transferTo(...)
上阅读了Java 10文档 ,它说:
Reads all characters from this reader and writes the characters to the given writer in the order that they are read 读取此阅读器中的所有字符,并按照读取的顺序将字符写入给定的编写器
The method transferTo
in Reader
would be very useful as currently it is quite verbose to copy data from reader to writer. Reader
的方法transferTo
非常有用,因为目前将数据从阅读器复制到编写器非常冗长。 As we mostly use InputStream
and OutputStream
in real life applications, is there a similar method for them? 由于我们在实际应用程序中大多使用InputStream
和OutputStream
,它们是否有类似的方法?
There is such method in InputStream
since Java 9: InputStream.transferTo() 自Java 9以来, InputStream
有这样的方法: InputStream.transferTo()
Also, for earlier versions of JDK, there is IOUtils.copy(InputStream input, OutputStream output)
in apache-commons-io library. 此外,对于早期版本的JDK, apache-commons-io库中有IOUtils.copy(InputStream input, OutputStream output)
。
Documentation says: 文件说:
Copies bytes from an InputStream to an OutputStream 将字节从InputStream复制到OutputStream
So, it should do the same thing. 所以,它应该做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.