platform-independent /dev/null output sink for Java(独立于平台的 Java 的/dev/null 输出接收器)
问题描述
除了匿名类 (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?'
我有一个程序可以对文件进行一致性分析.它有一个详细"选项.当详细选项打开时,我想看到很多输出.该程序并不着急,它是一个工具,因此与其编写所有这些额外的 if 语句来测试我是否需要输出,我只想在不需要时将其写入位桶.
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 的 NullOutputStreamhttps://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 输出接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:独立于平台的 Java 的/dev/null 输出接收器
基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
