问题

我需要为我正在使用的API指定一个OutputStream,但我实际上并不需要输出。 Java是否具有OutputStream等效于> /dev/null


#1 热门回答(101 赞)

/**Writes to nowhere*/
public class NullOutputStream extends OutputStream {
  @Override
  public void write(int b) throws IOException {
  }
}

#2 热门回答(58 赞)

Java看起来并不像Apache Commons IO那样。看看以下内容:
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/index.html
希望有所帮助。


#3 热门回答(38 赞)

它还没有被提及,所以我也会添加GuavaByteStreams.nullOutputStream(),因为有些人可能更喜欢Guava而不是Apache Commons IO或已经在他们的项目中使用它。

**注释:**如果你使用的是较旧版本的Guava(从1.0到13.0),则需要使用com.google.io.NullOutputStream


原文链接