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

有没有简单的方法来避免处理文本编码问题?


You can't really avoid dealing with the text encoding issues, but there are existing solutions:

  • Reader to InputStream : ReaderInputStream
  • Writer to OutputStream : WriterOutputStream
  • You just need to pick the encoding of your choice.


    如果你从一个字符串开始,你也可以执行以下操作:

    new ByteArrayInputStream(inputString.getBytes("UTF-8"))
    

    Well, a Reader deals with characters and an InputStream deals with bytes. The encoding specifies how you wish to represent your characters as bytes, so you can't really ignore the issue. As for avoiding problems, my opinion is: pick one charset (eg "UTF-8") and stick with it.

    Regarding how to actually do it, as has been pointed out, "the obvious names for these classes are ReaderInputStream and WriterOutputStream ." Surprisingly, "these are not included in the Java library" even though the 'opposite' classes, InputStreamReader and OutputStreamWriter are included.

    So, lots of people have come up with their own implementations, including Apache Commons IO. Depending on licensing issues, you will probably be able to include the commons-io library in your project, or even copy a portion of the source code (which is downloadable here).

  • Apache ReaderInputStream: API / source code direct link
  • Apache WriterOutputStream: API / source code direct link
  • As you can see, both classes' documentation states that "all charset encodings supported by the JRE are handled correctly".

    NB A comment on one of the other answers here mentions this bug. But that affects the Apache Ant ReaderInputStream class (here), not the Apache Commons IO ReaderInputStream class.

    链接地址: http://www.djcxy.com/p/13752.html

    上一篇: 如何在Python中将字符串转换为小写

    下一篇: 如何将Reader转换为InputStream并将Writer转换为OutputStream?