简体   繁体   English

什么是Java 10中的java.io.Reader transferTo(java.io.Writer)方法? InputStream有类似的方法吗?

[英]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? 由于我们在实际应用程序中大多使用InputStreamOutputStream ,它们是否有类似的方法?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM