是否有比以下更简洁/标准的习语(例如,JDK 方法)用于将输入“管道”到 Java 中的输出?
public void pipe(Reader in, Writer out) {
CharBuffer buf = CharBuffer.allocate(DEFAULT_BUFFER_SIZE);
while (in.read(buf) >= 0 ) {
out.append(buf.flip());
buf.clear();
}
}
[编辑] 请注意Reader
and Writer
are given。正确答案将演示如何采用in
和out
形成管道(最好不超过 1 或 2 个方法调用)。我会接受答案 where in
and out
are an InputStream
and an OutputStream
(最好是从/到Reader
/的转换Writer
)。in
如果orout
是/或/的子类,我将不接受答案。Reader
InputStream
Writer
OutputStrem