简体   繁体   English

BufferedReader 到 BufferedWriter

[英]BufferedReader to BufferedWriter

How can I obtain a BufferedWriter from a BufferedReader ?如何从BufferedReader BufferedWriter

I'd like to be able to do something like this:我希望能够做这样的事情:

BufferedReader read  = new BufferedReader(new InputStreamReader(...));
BufferedWriter write = new BufferedWriter(read);

You can use the following from Apache commons io:您可以使用 Apache commons io 中的以下内容:

IOUtils.copy(reader, writer);

site here网站在这里

JAVA 9 Updates JAVA 9 更新

Since Java 9, Reader provides a method called transferTo with the following signature:从 Java 9 开始,Reader 提供了一个名为 transferTo 的方法,其签名如下:

public long transferTo(Writer out)  throws IOException

As the documentation states, transferTo will:正如文档所述, transferTo 将:

Reads all characters from this reader and writes the characters to the given writer in the order that they are read.从该读取器读取所有字符,并按照读取顺序将字符写入给定写入器。 On return, this reader will be at end of the stream.返回时,此阅读器将位于 stream 的末尾。 This method does not close either reader or writer.此方法不会关闭读取器或写入器。 This method may block indefinitely reading from the reader, or writing to the writer.此方法可能会无限期地阻止从读取器读取或写入写入器。 The behavior for the case where the reader and/or writer is asynchronously closed, or the thread interrupted during the transfer, is highly reader and writer specific, and therefore not specified.读取器和/或写入器异步关闭或传输期间线程中断的情况的行为是高度特定于读取器和写入器的,因此未指定。

If an I/O error occurs reading from the reader or writing to the writer, then it may do so after some characters have been read or written.如果从读取器读取或写入写入器发生 I/O 错误,则可能在读取或写入某些字符后发生。 Consequently the reader may not be at end of the stream and one, or both, streams may be in an inconsistent state.因此,阅读器可能不在 stream 的末尾,并且一个或两个流可能位于不一致的 state 中。 It is strongly recommended that both streams be promptly closed if an I/O error occurs.如果发生 I/O 错误,强烈建议立即关闭两个流。

So in order to write contents of a Java Reader to a Writer, you can write:因此,为了将 Java Reader 的内容写入 Writer,您可以编写:

reader.transferTo(writer);

If you want to know what happens:如果你想知道会发生什么:

All input from the reader is copied to the inputstream来自阅读器的所有输入都被复制到输入流

Something similar too:也有类似的东西:

 private final void copyInputStream( InputStreamReader in, OutputStreamWriter out )      throws IOException
  {
    char[] buffer=new char[1024];
    int len;
    while ( ( len=in.read(buffer) ) >= 0 )
      {
      out.write(buffer, 0, len);
      }
  }

More on input and output on The Really big Index更多关于输入和 output 的真正大索引

BufferedWriter constructor is not overloaded for accept readers right? BufferedWriter 构造函数没有为接受读者重载,对吗? what Buhb said was correct.布赫说的是对的。

BufferedWriter  writer = new BufferedWriter(
new FileWriter("filename_towrite"));    

IOUtils.copy(new InputStreamReader(new FileInputStream("filename_toread")), writer);
writer.flush();
writer.close();

You could use Piped Read/Writers ( link ).您可以使用管道读/写器(链接)。 This is exactly what they're designed for.这正是它们的设计目的。 Not sure you could retcon them onto an existing buffered reader you got passed tho'.不确定您是否可以将它们重新配置到您通过的现有缓冲阅读器上。 You'd have to construct the buf reader yourself around it deliberately.您必须自己围绕它构建 buf 阅读器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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