我正在寻找神奇的 Java 类,它可以让我做这样的事情:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
FileOutputStream fileStream = new FileOutputStream(new File("/tmp/somefile"));
MultiOutputStream outStream = new MultiOutputStream(byteStream, fileStream);
outStream.write("Hello world".getBytes());
基本上,我想要
tee
来表示 Java 中的 OutputStream
。有什么想法吗?
谢谢!
自己动手。根本没有任何魔法。使用 Apache 的 TeeOutputStream 您基本上可以使用下面的代码。当然,使用 Apache Commons I/O 库,您可以利用其他类,但有时“实际上”为自己编写一些东西是很好的。 :)
public final class TeeOutputStream extends OutputStream {
private final OutputStream out;
private final OutputStream tee;
public TeeOutputStream(OutputStream out, OutputStream tee) {
if (out == null)
throw new NullPointerException();
else if (tee == null)
throw new NullPointerException();
this.out = out;
this.tee = tee;
}
@Override
public void write(int b) throws IOException {
out.write(b);
tee.write(b);
}
@Override
public void write(byte[] b) throws IOException {
out.write(b);
tee.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
tee.write(b, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
tee.flush();
}
@Override
public void close() throws IOException {
try {
out.close();
} finally {
tee.close();
}
}
}
使用上面的类进行测试,如下
public static void main(String[] args) throws IOException {
TeeOutputStream out = new TeeOutputStream(System.out, System.out);
out.write("Hello world!".getBytes());
out.flush();
out.close();
}
会打印
Hello World!Hello World!
。
(注意:被覆盖的
close()
可能需要一些小心:)
package Core;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class MultiOutputStream extends OutputStream {
private List<OutputStream> out;
public MultiOutputStream(List<OutputStream> outStreams) {
this.out = new LinkedList<OutputStream>();
for (Iterator<OutputStream> i = outStreams.iterator(); i.hasNext();) {
OutputStream outputStream = (OutputStream) i.next();
if(outputStream == null){
throw new NullPointerException();
}
this.out.add(outputStream);
}
}
@Override
public void write(int arg0) throws IOException {
for (Iterator<OutputStream> i = out.iterator(); i.hasNext();) {
OutputStream var = (OutputStream) i.next();
var.write(arg0);
}
}
@Override
public void write(byte[] b) throws IOException{
for (Iterator<OutputStream> i = out.iterator(); i.hasNext();) {
OutputStream var = (OutputStream) i.next();
var.write(b);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException{
for (Iterator<OutputStream> i = out.iterator(); i.hasNext();) {
OutputStream var = (OutputStream) i.next();
var.write(b, off, len);
}
}
@Override
public void close() throws IOException{
for (Iterator<OutputStream> i = out.iterator(); i.hasNext();) {
OutputStream var = (OutputStream) i.next();
var.close();
}
}
@Override
public void flush() throws IOException{
for (Iterator<OutputStream> i = out.iterator(); i.hasNext();) {
OutputStream var = (OutputStream) i.next();
var.flush();
}
}
}
到目前为止工作正常,只是测试了一些基本操作,例如从 System.out Stream 设置一个 MultiOutputStream 和 2 个 PrintStream,每个 PrintStream 写入一个单独的日志。 我用过
System.setOut(multiOutputStream);
写入我的终端屏幕和两个日志,没有任何问题。
ArrayList<OutputStream>
或当今流行的任何东西来存储您想要的所有流,并编写
write
方法来循环所有流,写入每个流。 public static void main(String[] args) throws IOException {
System.setOut(new PrintStream(new TeeOutputStream(System.out, new PrintStream("out.log"))));
System.setErr(new PrintStream(new TeeOutputStream(System.err, new PrintStream("err.log"))));
...