Java – Reader to InputStream – Java –阅读器到InputStream

最后修改: 2014年 7月 13日

中文/混合/英文(键盘快捷键:t)

In this quick tutorial we’re going to look at the conversion from a Reader to an InputStream – first with plain Java, then with Guava and finally with the Apache Commons IO library.

在这个快速教程中,我们将看看ReaderInputStream的转换–首先使用普通Java,然后使用Guava,最后使用Apache Commons IO库。

This article is part of the “Java – Back to Basic” series here on Baeldung.

本文是Baeldung网站上“Java – Back to Basic “系列的一部分。

1. With Java

1.使用Java

Let’s start with the Java solution:

让我们从Java的解决方案开始。

@Test
public void givenUsingPlainJava_whenConvertingReaderIntoInputStream_thenCorrect() 
  throws IOException {
    Reader initialReader = new StringReader("With Java");

    char[] charBuffer = new char[8 * 1024];
    StringBuilder builder = new StringBuilder();
    int numCharsRead;
    while ((numCharsRead = initialReader.read(charBuffer, 0, charBuffer.length)) != -1) {
        builder.append(charBuffer, 0, numCharsRead);
    }
    InputStream targetStream = new ByteArrayInputStream(
      builder.toString().getBytes(StandardCharsets.UTF_8));

    initialReader.close();
    targetStream.close();
}

Notice that we’re reading (and writing) chunks of data at a time.

请注意,我们每次都是在读(和写)数据块。

2. With Guava

2.有番石榴

Next – let’s look at the much simpler Guava solution:

接下来–让我们看看更简单的Guava解决方案

@Test
public void givenUsingGuava_whenConvertingReaderIntoInputStream_thenCorrect() 
  throws IOException {
    Reader initialReader = new StringReader("With Guava");

    InputStream targetStream = 
      new ByteArrayInputStream(CharStreams.toString(initialReader)
      .getBytes(Charsets.UTF_8));

    initialReader.close();
    targetStream.close();
}

Notice that we’re using an out of the box input stream which turns the entire conversion into a one liner.

请注意,我们使用的是一个开箱即用的输入流,它将整个转换过程变成了一个单行道。

3. With Commons IO

3.使用Commons IO

Finally – let’s look at couple of Commons IO solutions – also simple one liners.

最后–让我们看看几个Commons IO解决方案–也是简单的一句话。

First, using ReaderInputStream:

首先,使用ReaderInputStream

@Test
public void givenUsingCommonsIOReaderInputStream_whenConvertingReaderIntoInputStream() 
  throws IOException {
    Reader initialReader = new StringReader("With Commons IO");

    InputStream targetStream = new ReaderInputStream(initialReader, Charsets.UTF_8);

    initialReader.close();
    targetStream.close();
}

Lastly, the same conversion using IOUtils:

最后,使用IOUtils进行同样的转换。

@Test
public void givenUsingCommonsIOUtils_whenConvertingReaderIntoInputStream() 
  throws IOException {
    Reader initialReader = new StringReader("With Commons IO");

    InputStream targetStream = 
      IOUtils.toInputStream(IOUtils.toString(initialReader), Charsets.UTF_8);

    initialReader.close();
    targetStream.close();
}

Note that we’re here dealing with any kind of Reader – but if you’re working specifically with text data, it’s always a good idea to specify the charset explicitly rather than use the JVM default.

请注意,我们在这里处理的是任何类型的Reader–但如果你是专门处理文本数据,明确指定字符集而不是使用JVM的默认值总是一个好主意。

4. Conclusion

4.总结

And there you have it – 3 simple ways to transform the Reader into an InputStream. Make sure to check out the sample over on GitHub.

这就是你所拥有的–将Reader转换为InputStream的3种简单方法。请确保查看GitHub上的样本