Java的平台无关/ dev / null输出接收器 [英] platform-independent /dev/null output sink for Java
问题描述
除了匿名类( new OutputStream(){...}
)之外,任何人都可以建议道德相当于 new FileOutputStream( / dev / null)
哪个也适用于Windows?
Other than an anonymous class (new OutputStream() { ... }
), can anyone suggest a moral equivalent of new FileOutputStream("/dev/null")
that also works on Windows?
如果有人想知道'这是为了什么?'
In case someone's wondering 'what's this for?'
我有一个程序可以对文件进行一致性分析。它有一个'详细'选项。当verbose选项打开时,我想看到很多输出。该程序并不匆忙,它是一个工具,所以不是编写所有额外的 if
语句来测试我是否需要输出,我只想将它写入不需要时使用bit-bucket。
I have a program that does a consistency analysis on a file. It has a 'verbose' option. When the verbose option is on, I want to see a lot of output. The program is not in a hurry, it's a tool, so instead of writing all those extra if
statements to test if I want the output, I just want to write it to the bit-bucket when not desired.
推荐答案
你可以在apache commons中使用NullOutputStream
https://commons.apache.org /proper/commons-io/javadocs/api-2.4/org/apache/commons/io/output/NullOutputStream.html
You can use NullOutputStream from apache commons https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/output/NullOutputStream.html
或者只是实施自己的
package mypackage;
import java.io.OutputStream;
import java.io.IOException;
public class NullOutputStream extends OutputStream {
public void write(int i) throws IOException {
//do nothing
}
}
这篇关于Java的平台无关/ dev / null输出接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!