InputStream to String Conversion Example
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 String while 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 log Strings 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 of the 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.
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.
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.
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
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:
String stringFromStream = CharStreams.toString(new InputStreamReader(fis, "UTF-8"));
Regarding dependency, You need to include guava library i.e. guava-11.0.1.jar in your project classpath. here is full code example:
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.
Related Java tutorials and Examples