Is there a Null OutputStream in Java?

前端 未结 11 666
灰色年华
灰色年华 2020-12-08 13:04

I need to specify an OutputStream for an API I\'m using, but I don\'t actually have a need for the output. Does Java have an OutputStream equivale

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 13:51

    There is new boy in the town, that takes care of this like a charm, just few lines of code should work. Its JDK 11 and nullWriter() has been introduced there, that takes care of this. Here goes the code to deal with same old problem, new way without worrying about Operating System(OS).

    String fileContent = "Welcome to StackOverflow readers !! Here goes the question link...";
    Writer writer = Writer.nullWriter();
    writer.write(fileContent);
    writer.close();
    

    Hope this may help someone!

提交回复
热议问题