This tutorial shows 5 different ways to convert InputStream to String in Java.
Used in this tutorial
- Stream API (JDK 1.8)
- Apache Commons IO (2.5)
- Guava (20.0)
Table of contents
The tutorial source code is available on Github. Download the source
Native Java
java.io.BufferedReader
The first step to convert an InputStream to String in Java is by using java.io.BufferedReader. Below the Java code :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public String convert(InputStream inputStream, Charset charset) throws IOException { StringBuilder stringBuilder = new StringBuilder(); String line = null; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) { while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } } return stringBuilder.toString(); } |
Try-with-resources is used here to allow the automatic closing of the BufferedReader. It will only compile with a version equal to or greater than 1.7. Put the bufferedReader instantiation inside the try block if you are using a version prior to 1.7.
java.util.Scanner
java.util.Scanner provides a much more easier way to convert an InputStream to String as below :
1 2 3 4 5 6 |
public String convert(InputStream inputStream, Charset charset) throws IOException { try (Scanner scanner = new Scanner(inputStream, charset.name())) { return scanner.useDelimiter("\\A").next(); } } |
Java 1.8
Stream API
You can use Collectors of the Java Stream API to make the InputStream to String conversion. Below the code to use :
1 2 3 4 5 6 |
public String convert(InputStream inputStream, Charset charset) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset))) { return br.lines().collect(Collectors.joining(System.lineSeparator())); } } |
Third-party libraries
Apache Commons IO
Apache Commons IO provides the utility class org.apache.commons.io.IOUtils to convert an InputStream. You need first to add the dependency to work classpath :
1 2 3 4 5 |
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> |
1 |
compile group: 'commons-io', name: 'commons-io', version: '2.5' |
Below how to use it :
1 2 3 |
public String convert(InputStream inputStream, Charset charset) throws IOException { return IOUtils.toString(inputStream, charset); } |
Guava
Google Guava provides also a powerful way to easily convert an inputStream to a String Java object. Download first the dependency
1 2 3 4 5 |
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency> |
1 |
compile group: 'com.google.guava', name: 'guava', version: '20.0' |
and use com.google.common.io.CharStreams as shown below :
1 2 3 |
public String convert(InputStream inputStream, Charset charset) throws IOException { return CharStreams.toString(new InputStreamReader(inputStream, charset)); } |
Test the code
Below a JUnit code testing the different conversions :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.roufid.tutorials; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.roufid.tutorials.converter.InputStreamToStringConverter; @RunWith(SpringRunner.class) @SpringBootTest public class InputstreamToStringTutorialApplicationTests { Charset charset = StandardCharsets.UTF_8; public static final String HELLO_WORLD = "Hello world from France"; InputStream inputStream = new ByteArrayInputStream(HELLO_WORLD.getBytes(charset)); @Autowired @Qualifier("nativeConversion") private InputStreamToStringConverter nativeConverter; @Autowired @Qualifier("scannerConversion") private InputStreamToStringConverter scannerConverter; @Autowired @Qualifier("streamConversion") private InputStreamToStringConverter streamConverter; @Autowired @Qualifier("guavaConversion") private InputStreamToStringConverter guavaConverter; @Autowired @Qualifier("apacheCommonsConversion") private InputStreamToStringConverter apacheCommonsConverter; @Test public void nativeConversion() throws IOException { String result = nativeConverter.convert(inputStream, charset); Assert.assertEquals(HELLO_WORLD, result); } @Test public void scannerConversion() throws IOException { String result = scannerConverter.convert(inputStream, charset); Assert.assertEquals(HELLO_WORLD, result); } @Test public void streamConversion() throws IOException { String result = streamConverter.convert(inputStream, charset); Assert.assertEquals(HELLO_WORLD, result); } @Test public void guavaConversion() throws IOException { String result = guavaConverter.convert(inputStream, charset); Assert.assertEquals(HELLO_WORLD, result); } @Test public void apacheCommonsConversion() throws IOException { String result = apacheCommonsConverter.convert(inputStream, charset); Assert.assertEquals(HELLO_WORLD, result); } } |
The tutorial source code is available on Github. Download the source
References