[英]PrintWriter to multiple files
我需要將相同的文本寫入多個文件(或流)。 有時我需要使用Writer,有時需要PrintWriter,有時需要OutputStream ...
一種解決方法是將PrintWriter擴展為具有PrintWriters數組,並按如下方法重寫每個方法:
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相同)
有更好的選擇嗎? 這已經在庫中實現了嗎?
您不需要重寫PrintWriter
所有方法,因為所有printXyz
方法都委托給Writer
API的基本write
方法。
雖然: PrintWriter
具有構造函數PrintWriter(Writer out)
。 因此,您只需要使用_one_method來實現一個新的Writer
,如下所示:
public class MultiWriter implements Writer {
private List<Writer> delegates;
public MultiWriter(List<Writer> delegates){
this.delegates = delegates;
}
public void write(char cbuf[], int off, int len) throws IOException {
for(Writer w: delegates){
w.writer(cbuf, off, len);
}
}
}
像這樣使用:
PrintWriter myPrinter = new PrintWriter(new MultiWriter(listOfDelegates));
myPrintWriter.println("Hello World!");
這將照顧Writers
。
您可以使用OutputStream
進行相同的操作。 您也可以只實現MultiOutputStream
,省略MultiWriter
並使用委托鏈PrintWriter->OutputStreamWriter->MultiOutputStream
。 這只是一個類中的一種實現方法,您可以免費獲得PrintWriter
, Writer
和OutputStream
。
已經有用於此的庫。 如果可以使用OSS,請獲取Apache Commons IO ,然后看看TeeOutputStream類。 這是一些示例代碼,說明其用法:
public class TeeOutputStreamTest {
@Test
public void testPrintToMultipleStreams() throws Exception {
final String fileName1 = "/tmp/fileOne.txt";
final String fileName2 = "/tmp/fileTwo.txt";
final String fileName3 = "/tmp/fileThree.txt";
final TeeOutputStream tos = new TeeOutputStream(new FileOutputStream(
fileName1), new TeeOutputStream(new FileOutputStream(fileName2),
new FileOutputStream(fileName3)));
final PrintWriter writer = new PrintWriter(tos);
writer.println("Hello World");
writer.close();
}
}
您可以在接受常規OutputStream
任何地方使用TeeOutputStream
,也可以根據需要將其包裝在Writer
。
使用日志記錄庫並在其配置中定義多個追加程序。
您可以使用Apache Log4J或LogBack達到這種效果(我建議使用LogBack,但要分別使用它們)。
不幸的是,您的解決方案是最好的。
有一個替代方法,但這並不漂亮。 如果您被迫傳遞PrintWriter,而沒有提供在加載時添加到JRE的受信任庫中的擴展,以用實際上執行您所建議的類替換 PrintWriter
,那么我認為您沒有太多選擇。
好吧,實際上您可以輕松地做到這一點:
而不是重寫PrintWriter
類的所有方法,您可以僅重寫Writer
類的一個方法 :
public void write(char cbuf[], int off, int len);
因為所有其他方法( print(...)
和其他write(...)
s)都使用此方法:
public void write(char cbuf[], int off, int len) {
for (Writer out : outs) out.write(cbuf, off, len);
}
UPD:也是flush()
和close()
方法。
對於OutputStream
,與方法public void write(int b)
相同的情況
我更喜歡構圖而不是擴展。 這是另一個選擇:
public class MultiWriter { List<PrintWriter> listPrintWriter = new LinkedList<PrintWriter>; List<Writer> listWriter = new LinkedList<Writer>; public void addPrintWriter(final PrintWriter newPrintWriter) { listPrintWriter.add(newPrintWriter); } public void addWriter(final Writer newWriter) { listWriter.add(newWriter); } public void write((char cbuf[], int off, int len) { if (listPrintWriter != null) { for (PrintWriter printWriter : listPrintWriter) { printWriter.write(cbuf, off, len); } } if (listWriter != null) { for (Writer writer : listWriter) { writer.write(cbuf, off, len); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.