How to Convert an InputStream Into a String in Java
-
Use
Stream
API to Convert anInputStream
to a String -
Use
ByteArrayOutputStream
to Read or Convert an InputStream to String -
Use
IOUtils.toString
of Apache Commons to Read or Convert an InputStream to a String
In this tutorial, we will talk about how to convert an InputStream to a String in Java. An InputStream is a stream of bytes that can be further used to perform several tasks like reading. In general, it is a class that contains everything in bytes. If we want to convert this stream of bytes to any other type of data, we may have to use specific methods.
Here we will go through the several ways by which we can read or convert an InputStream
into a string in Java
Use Stream
API to Convert an InputStream
to a String
We can convert an InputStream
to a string using the Stream
API that is a part of Java 8. InputStreamReader
reads the inputStream and BufferedReader().lines()
helps us to convert this InputStream
into a stream of String
. As we can see, Collectors.joining()
is being used to join all the strings in the stream and then return a single string.
package com.company;
import java.io.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
public class Main {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("java/sampleFile.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
Stream<String> streamOfString = new BufferedReader(inputStreamReader).lines();
String streamToString = streamOfString.collect(Collectors.joining());
System.out.println(streamToString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
This is a sample text file
Use ByteArrayOutputStream
to Read or Convert an InputStream to String
We know that an InputStream
is made up of bytes, and thus we can ByteArrayOutputStream
class that converts the readInputStream
into an array of bytes. After that, we can convert this array of bytes into a String using toString()
.
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
InputStream readInputStream = new FileInputStream("java/sampleFile.txt");
String encoding = "UTF-8";
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] byteSize = new byte[1024];
int length;
while ((length = readInputStream.read(byteSize)) != -1) {
byteArrayOutputStream.write(byteSize, 0, length);
}
System.out.println(byteArrayOutputStream.toString(encoding));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
This is a sample text file
Use IOUtils.toString
of Apache Commons to Read or Convert an InputStream to a String
To make our task easy, we can use the IOUtils.toString
function included in the Apache Commons library. To use the Apache Commons Library, we can include this dependency in our project.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
In the example below, first, we create an InputStream
which is a text file, and then call the IOUtils.toString()
method that takes an InputStream
and the encoding to use to convert the stream. We are using UTF-8 encoding, which is universally used.
package com.company;
import java.io.*;
import org.apache.commons.io.IOUtils;
public class Main {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("java/sampleFile.txt");
String steamToString = IOUtils.toString(inputStream, "UTF-8");
System.out.println(steamToString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
This is a sample text file
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java String
- How to Perform String to String Array Conversion in Java
- How to Remove Substring From String in Java
- How to Convert Byte Array in Hex String in Java
- How to Convert Java String Into Byte
- How to Generate Random String in Java
- The Swap Method in Java