How to convert a Reader to InputStream and a Writer to OutputStream?

前端 未结 12 2152
慢半拍i
慢半拍i 2020-12-02 07:31

Is there an easy way to avoid dealing with text encoding problems?

12条回答
  •  盖世英雄少女心
    2020-12-02 08:05

    Also note that, if you're starting off with a String, you can skip creating a StringReader and create an InputStream in one step using org.apache.commons.io.IOUtils from Commons IO like so:

    InputStream myInputStream = IOUtils.toInputStream(reportContents, "UTF-8");
    

    Of course you still need to think about the text encoding, but at least the conversion is happening in one step.

提交回复
热议问题