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

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

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

相关标签:
12条回答
  • 2020-12-02 07:52

    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 (e.g. "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".

    N.B. 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.

    0 讨论(0)
  • 2020-12-02 07:54

    The obvious names for these classes are ReaderInputStream and WriterOutputStream. Unfortunately these are not included in the Java library. However, google is your friend.

    I'm not sure that it is going to get around all text encoding problems, which are nightmarish.

    There is an RFE, but it's Closed, will not fix.

    0 讨论(0)
  • 2020-12-02 08:00

    For Reading a string in a stream using just what java supplies.

    InputStream s = new BufferedInputStream( new ReaderInputStream( new StringReader("a string")));
    
    0 讨论(0)
  • 2020-12-02 08:04

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

    • Reader to InputStream: ReaderInputStream
    • Writer to OutputStream: WriterOutputStream

    You just need to pick the encoding of your choice.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 08:06

    You can't avoid text encoding issues, but Apache commons-io has

    • ReaderInputStream
    • WriterOutputStream

    Note these are the libraries referred to in Peter's answer of koders.com, just links to the library instead of source code.

    0 讨论(0)
提交回复
热议问题