In this tutorial, you will learn how to convert an InputStream into a String in Java. The tutorial will cover the recommended approaches and the most straightforward ones.
For those new to InputStream, it is a class used to read bytes of data. These data can be from a file, network, or from memory. In this tutorial, you will read bytes of data from a file and convert the data to a String.
Create a Maven project
Some of the examples that you will cover leverage third-party libraries. As a result, you need to add the dependencies to the project.
To achieve this, start by creating a Maven project. Next, copy and paste the following dependencies into the pom.xml file.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.1.2-jre</version> </dependency>
Create a file and add text data
Create a file named data.txt under the root of your project. Copy and paste the following text into the file.
In this tutorial, you will learn how to convert an InputStream to a String in Java.
The following image shows where you should create the file.

All the examples in this tutorial will use an InputStream to read the text data from the file.
Using the String class
There are different ways you can use to create a String in Java. When creating a new instance of a class you can pass the array of bytes as the argument. As a result, this converts the array of bytes to a String. Let’s look at an example to understand how this works.
package com.javawhizz; import java.io.FileInputStream; import java.io.IOException; public class Main { public static void main(String[] args){ try (FileInputStream inputStream = new FileInputStream("data.txt")){ String data = new String(inputStream.readAllBytes()); System.out.println(data); }catch (IOException e){ e.printStackTrace(); } } }
The InputStream
class is an abstract class for all InputStream
classes. One of the implementations of this class is the FileInputStream
.
Since you are reading data from a file, you should use this class. Nonetheless, It is a good practice to use FileReader
to read text data from a file. The reason for this is that FileInpuStream
is best suited for reading raw bytes. One such example is an image.
After creating an instance of a FileInputStream
. Pass the file name as the argument. Next, Create a new instance of the String class and pass the readAllBytes()
method as the argument.
As a result, the String class will create a String representation of the bytes in the data.txt file.
Output
In this tutorial, you will learn how to convert an InputStream to a String in Java.
Using IOUtils.toString() method
The IOUtils
class comes from the Apache Commons IO library. This has a utility method that you can leverage to convert an array of bytes to a String. The method referred to in this case is the toString()
method. Here is an example:
package com.javawhizz; import org.apache.commons.io.IOUtils; import java.io.FileInputStream; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args){ try (FileInputStream inputStream = new FileInputStream("data.txt")){ String data = IOUtils .toString(inputStream, StandardCharsets.UTF_8); System.out.println(data); }catch (Exception e){ e.printStackTrace(); } } }
After creating an InputStream
for the file, call the IOUtils.toString()
static method. Next, pass the InputStream and StandardCharset.UTF_8
as the arguments of the method.
As a result, the method will read the contents of the InputStream
as String. Note that the method will use UTF_8
as the encoding system.
Output
In this tutorial, you will learn how to convert an InputStream to a String in Java.
Using CharStreams.toString() method
Guava provides Google core libraries for the Java API. One of the classes that comes from this library is the CharStreams
. Generally, you can leverage its toString()
method to convert an InputStream
to a String. Here is an example:
package com.javawhizz; import com.google.common.io.CharStreams; import java.io.FileInputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args){ try (FileInputStream inputStream = new FileInputStream("data.txt")){ String data = CharStreams.toString(new InputStreamReader( inputStream, StandardCharsets.UTF_8)); System.out.println(data); }catch (Exception e){ e.printStackTrace(); } } }
This example is a bit different from the previous one as the parameter expects a Reader
. Pass an instance of InputStreamReader
which which is an implementation a Reader
.
Additionally, pass FileInputStream
and StandardCharsets.UTF_8
as the argument of the InputStreamReader
. As a result, the toString()
method will use the contents of the FileInputStream
to create a String.
Output
In this tutorial, you will learn how to convert an InputStream to a String in Java.
Using IOUtils.copy() method
Apart from the toString()
method of IOUtils
, you can also leverage its copy method. This method copies the contents of an InputStream
to a Writer
. As a result, you can get the resulting data from the Writer
. Here is an example:
package com.javawhizz; import org.apache.commons.io.IOUtils; import java.io.FileInputStream; import java.io.StringWriter; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args){ try (FileInputStream inputStream = new FileInputStream("data.txt")){ StringWriter stringWriter = new StringWriter(); IOUtils.copy(inputStream, stringWriter, StandardCharsets.UTF_8); System.out.println(stringWriter); }catch (Exception e){ e.printStackTrace(); } } }
Since you want to get the contents of the InputStream
as String
, define a Writer
of type StringWriter
. This writer will help to create a String.
Next, call the IOUtils.copy()
method and pass the expected arguments. Pass the references to FileInputStream
, StringWriter
, and StandardCharsets.UTF_8
.
As a result, the method will copy the FileInputStream
contents to the StringWriter
. Note that since we are using a StringWriter
, the contents of the file will result in a String
.
Output
In this tutorial, you will learn how to convert an InputStream to a String in Java.
Conclusion
In this tutorial, you have learned how to convert an InputStream
into a String in Java. Note that these are not the only approaches you can use to convert an InputStream
to a String. Nonetheless, these are the recommended and most straightforward ones.
Happy Hacking!
0 Comments