首页javareaderJava I/O - 如何从Reader读取并写入Writer,直到没有更多的reader输入

Java I/O - 如何从Reader读取并写入Writer,直到没有更多的reader输入

我们想知道如何从Reader读取并写入Writer,直到没有更多的reader输入。
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Main {
  /**
   * Read input from reader and write it to writer until there is no more
   * input from reader.
   *
   * @param reader the reader to read from.
   * @param writer the writer to write to.
   * @param buf the char array to use as a bufferx
   */
  public static void flow( Reader reader, Writer writer, char[] buf ) 
      throws IOException {
      int numRead;
      while ( (numRead = reader.read(buf) ) >= 0) {
          writer.write(buf, 0, numRead);
      }
  }
}