我需要将相同的文本写入多个文件(或流)。有时我需要使用 Writer,有时是 PrintWriter,有时是 OutputStream...
一种方法是扩展 PrintWriter 以具有 PrintWriter 数组并覆盖每个方法,如下所示:
class MutiplePrintWriter extends PrintWriter {
private PrintWriter[] outs;
public MutiplePrintWriter(PrintWriter[] outs) { this.out = out; }
public void print(boolean b) { for (PrintWriter out : outs) print(b); }
public void print(char c) { for (PrintWriter out : outs) print(c); }
public void print(char[] s) { for (PrintWriter out : outs) print(s); }
...
}
(对于 Writer、OutputStream 也是一样的......)
有更好的选择吗?这是否已经在库中实现?