Converting InputStream to String in Java has become very easy after introduction of Scanner class in Java 5 and due to development of several open source libraries like Apache commons IOUtils and Google Open source guava-libraries which provides excellent support to convert InputStream to String in Java program. we often need to convert InputStream to Stringwhile working in Java for example if you are reading XML files from InputStream and later performing XSLT transformation on it or if InputStream is reading data from text file or text input Source and we either want to logStrings in log file or want to operate on whole String. Before Java 5 you would have to write lots of boiler plate code to read String line by line or byte by byte depending upon whether you are using either BufferedReader or not but as I said since JDK 5 added Scanner for reading input, its fairly easy to convert InputStream into String.

Before Converting any InputStream or ByteStream into String don't forget to provide character encoding or charSet which tells Java Which characters to expect from those streams of bytes. in the absence of correct character encoding you might alter the output because same bytes can be used to represent different character in different encoding. Another thing to keep in mind is that if you don't provide character encoding, Default character encoding in Java will be used which can be specified from System property "file.encoding" or "UTF-8" if file.encoding is not specified. In this Java tutorial we will see 5 different example of converting InputStream to String in Java both by using standard JDK libraries and using open source libraries.

How to convert InputStream to String in Java – 5 Examples

here are different ways to convert InputStream to String in Java, first we will see most simple way of reading InputStream as String.

InputStream to String - Using Java 5 Scanner

java.util.Scanner has constructor which accept an InputStream, a character encoding and a delimiter to read String from InputStream. Here we have used delimiter as "\A" which is boundary match for beginning ofthe input as declared in java.util.regex.Pattern and that's why Scanner is returning whole String form InputStream. I frequently use this technique to read input from user in Java using System.in which is most common example of InputStream in Java, but as demonstrated here this can also be used to read text file in Java.

/**
* Java program example to demonstrate How to convert InputStream into String by using JDK
* Scanner utility. This program will work Java 5 onwards as Scanner was added in Java 5.
*/publicclass InputStreamTest {

Output:
This String is read from InputStream by changing InputStream to String in Java.

If you want to see how an incorrect character encoding completely changes the String just change the character encoding form "UTF-8" to "UTF-16" and you see Chinese(may be) characters instead of English.

Output:?????????????!???????????!??????^(4)????????

Convert InputStream to String - Plain old JDK4 Example

If you still need to do it on plain old Java on JDK4 without including any additional dependency in your PATH and Classpath than here is quick example using BufferedReader in Java, Remember you can also do this by reading byte by byte from InputStream but that's very slow so consider using BufferedReader for better performance even if you code on JDK4 . For more performance tips see my post 4 JDBC performance tips in Java program. Let’s see example of converting InputStream to String using BufferedReader in Java.

/**
* Java program to demonstrate How to read InputStream as String

* using BufferedReader and StringBuilder in Java.
* This is old and standard way of converting an InputStream into String in Java
*/publicstaticvoid main(String args[])throwsFileNotFoundException, UnsupportedEncodingException, IOException{FileInputStream fis = newFileInputStream("c:/sample.txt");StringBuilder inputStringBuilder = newStringBuilder();BufferedReader bufferedReader = newBufferedReader(newInputStreamReader(fis, "UTF-8"));String line = bufferedReader.readLine();while(line != null){
inputStringBuilder.append(line);inputStringBuilder.append('\n');
line = bufferedReader.readLine();}System.out.println(inputStringBuilder.toString());

}
Output:
This String is read from InputStream by changing InputStream to String in Java.second line

This is good plain old Java method of converting InputStream to String without adding any extra dependency but remember its converting \r to \n because we are reading line by line, which in most cases fine.

Read InputStream to String - Using Apache IOUtils library

As I said earlier there are many open source library in Java which makes coding lot more easier than any other language. Here is code example for How to convert InputStream to String in Java using Apache IOUtils

/**
* Example of How to read InputStream into String by using Apache IOUtils library.
* Nice and clean way of getting InputStream as String in Java
*/

Isn't it a compact way of converting InputStream to String in Java, just one line of code and that does take care of character encoding as well..

InputStream to String - Using Google's guava-libraries

Google has open source its own set of Java libraries they use for Java development inside Google. it has lots of utility function and also complements Apache commons package. Here is a quick way of converting InputStream to String using Google libraries:

How to read InputStream into String with IOUtils.copy and StringWriter class

java.io.StringWriter is another convenient way of reading writing Strings and by using IOUtils.copy() you can copy contents form InputStream to reader, Here is a complete code example of reading InputStream as String using StringWriter:

That's all on converting InputStream to String in Java by using standard core java library and by using open source Apache commons IOUtils and Google's guava libraries. Don't forget Character encoding when converting bytes to String which is case here also make a convenient choice as sometime adding additional library for one functionality is not the best option. let us know if you know any other way of converting InputStream to String in Java.

I think Java solution work for all kind of files including text and binary, but just remember to provide correct character encoding while reading data from stream, otherwise your code will use default character stream of platform, which may not be the same as of contents you are reading. Another thing, don't forget to close streams and files once you read data from them. This will release file descriptor associated with them.