How to convert InputStream to String in Java

In Java, you can use BufferedReader + InputStreamReader to convert InputStream to String.

InputStreamToStringExample.java

package com.mkyong.core;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamToStringExample {

	public static void main(String[] args) throws IOException {

		// intilize an InputStream
		InputStream is = 
                     new ByteArrayInputStream("file content..blah blah".getBytes());

		String result = getStringFromInputStream(is);

		System.out.println(result);
		System.out.println("Done");

	}

	// convert InputStream to String
	private static String getStringFromInputStream(InputStream is) {

		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();

		String line;
		try {

			br = new BufferedReader(new InputStreamReader(is));
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return sb.toString();

	}

}

Output


file content..blah blah
Done

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Abel Bottello
Guest
Abel Bottello

Thank you so much, finally I could print some text gotten from a GET request in Android…

Max
Guest
Max

There’s a much better trick that doesn’t require apache stuff.

 String str = new Scanner(inputStream).useDelimiter("\\A").next();

Essentially this tells the scanner to tokenize the stream until the end is reached, so next() returns the whole stream as a string.

Jonathan G Kline
Guest
Jonathan G Kline

In case you want to update and include a Java 8 version, this is your code but with a try-with-resources block instead of a try-catch-finally block:

try(BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)))
{
while((line = br.readLine()) != null)
{
sb.append(line);
}
} catch(IOException e)
{
e.printStackTrace();
}

Mali
Guest
Mali

Thanks a lot !

Umair Aslam
Guest
Umair Aslam

Is vice-versa possible i.e to write a String to InputStream in Java?

Guy
Guest
Guy

Are you sure using “sb.append(line);” won’t loose the “\n” at the end of each line?
According to the JDoc of br.readLine():
” * @return A String containing the contents of the line, not including
* any line-termination characters,”

Bhushan
Guest
Bhushan

It can easily done in 3 lines by using Guava library as shown in 5 ways to convert InputStream to String in Java

Cloud Computing
Guest
Cloud Computing

Good tutorial

Mirta - Recipes Cookbook
Guest
Mirta - Recipes Cookbook
Why don’t you better use the Java Standard support for the translation? I personally avoid Scanners when possible. public String inputStram2String(InputStream in) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int i; (i = in.read(b)) != -1;) { out.append(new String(b, 0, i)); } return out.toString(); }
Edward
Guest
Edward

I prefer to use IOUtils.toString(InputStream) from Apache Commons.