簡體   English   中英

兩個OutputStream而不是一個?

[英]Two OutputStream's instead of one?

我正在處理僅公開以下日志記錄配置的商業Java API:

cplex.setOut(OutputStream arg0);

我想記錄兩個流:文件和控制台。 可能嗎?

我相信是。

我會使用apache commons io lib

例如

FileOutputStream fos = ...;
TeeOutputStream brancher = TeeOutputStream(fos, System.out);
cplex.setOut(brancher);

編寫您自己的OutputStream實現,該實現將對write方法的調用委派給兩個包裝的OutputStream,一個用於控制台,一個用於文件。

簡單:

cplex.setOut(new OutputStream() {

    public void write(int b) throws IOException {
        outputStream1.write(b);
        outputStream2.write(b);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM