独立于平台的 Java 的/dev/null 输出接收器 [英] platform-independent /dev/null output sink for Java

查看:34
本文介绍了独立于平台的 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 输出接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆