Java – Convert InputStream to String

This tutorial shows several ways to convert an InputStream to a String in Java.

1- BufferedReader

The common way for generating a String out of an InputStream is through using BufferedReader as the following:

2- Scanner

Another common way is to use a Scanner object as the following:

3- Java 8

With Java 8, you can generate a String out of an InputStream using Collectors and collect() utility method:

4- Apache Commons library

Apart from JDK, you can use Apache Commons library for converting an InputStream to String using 2 ways:

1st way:

2nd way:

 

0 0 votes
Article Rating

Hussein Terek

Owner of programmergate.com, I have a passion for software engineering and everything related to Java environment.

guest
0 Comments
Inline Feedbacks
View all comments