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

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

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

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

    commons-io 2.0 has WriterOutputStream

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

    Use:

    new CharSequenceInputStream(html, StandardCharsets.UTF_8);
    

    This way does not require an upfront conversion to String and then to byte[], which allocates lot more heap memory, in case the report is large. It converts to bytes on the fly as the stream is read, right from the StringBuffer.

    It uses CharSequenceInputStream from Apache Commons IO project.

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

    A warning when using WriterOutputStream - it doesn't always handle writing binary data to a file properly/the same as a regular output stream. I had an issue with this that took me awhile to track down.

    If you can, I'd recommend using an output stream as your base, and if you need to write strings, use an OUtputStreamWriter wrapper around the stream to do it. It is far more reliable to convert text to bytes than the other way around, which is likely why WriterOutputStream is not a part of the standard Java library

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

    You can use Cactoos (no static methods, only objects):

    • new InputStreamOf(reader)
    • new OutputStreamTo(writer)

    You can convert the other way around too:

    • new ReaderOf(inputStream)
    • new WriterTo(outputStream)
    0 讨论(0)
  • 2020-12-02 08:15

    If you are starting off with a String you can also do the following:

    new ByteArrayInputStream(inputString.getBytes("UTF-8"))
    
    0 讨论(0)
  • 2020-12-02 08:18

    Are you trying to write the contents of a Reader to an OutputStream? If so, you'll have an easier time wrapping the OutputStream in an OutputStreamWriter and write the chars from the Reader to the Writer, instead of trying to convert the reader to an InputStream:

    final Writer writer = new BufferedWriter(new OutputStreamWriter( urlConnection.getOutputStream(), "UTF-8" ) );
    int charsRead;
    char[] cbuf = new char[1024];
    while ((charsRead = data.read(cbuf)) != -1) {
        writer.write(cbuf, 0, charsRead);
    }
    writer.flush();
    // don't forget to close the writer in a finally {} block
    
    0 讨论(0)
提交回复
热议问题