Write string to output stream

后端 未结 6 871
误落风尘
误落风尘 2020-12-04 07:34

I have several output listeners that are implementing OutputStream. It can be either a PrintStream writing to stdout or to a File, or it can be writing to memory or any othe

6条回答
  •  不知归路
    2020-12-04 08:34

    By design it is to be done this way:

    OutputStream out = ...;
    try (Writer w = new OutputStreamWriter(out, "UTF-8")) {
        w.write("Hello, World!");
    } // or w.close(); //close will auto-flush
    

提交回复
热议问题