Categories
inputstream io java stream string

How do I read / convert an InputStream into a String in Java?

4544

If you have a java.io.InputStream object, how should you process that object and produce a String?


Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file.

What is the easiest way to take the InputStream and convert it to a String?

public String convertStreamToString(InputStream is) {
    // ???
}

3

  • Does this answer your question? Scanner is skipping nextLine() after using next() or nextFoo()?

    Oct 8, 2020 at 14:02

  • 1

    Remember that you need to take the encoding of the input stream in consideration. The system default is not necessarily always the one you wan.t

    Oct 30, 2020 at 9:52

  • 10

    Most of these answers were written pre-Java 9, but now you can get a byte array from the InputStream using .readAllBytes. So, simply “new String(inputStream.readAllBytes())” works using String’s byte[] constructor.

    May 28, 2021 at 21:19

2733

A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter… something like

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

or even

// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding); 

Alternatively, you could use ByteArrayOutputStream if you don’t want to mix your Streams and Writers

2

  • Did the toString get deprecated? I see IOUtils.convertStreamToString()

    – RCB

    Jul 2, 2020 at 15:26

  • I added an edit to include a searchable link to the actual source code itself as a reference. I believe this augments the answer for those who want to see how the command works.

    – satchel

    Jul 6, 2021 at 0:03

2733

A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter… something like

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

or even

// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding); 

Alternatively, you could use ByteArrayOutputStream if you don’t want to mix your Streams and Writers

2

  • Did the toString get deprecated? I see IOUtils.convertStreamToString()

    – RCB

    Jul 2, 2020 at 15:26

  • I added an edit to include a searchable link to the actual source code itself as a reference. I believe this augments the answer for those who want to see how the command works.

    – satchel

    Jul 6, 2021 at 0:03

2345

Here’s a way using only the standard Java library (note that the stream is not closed, your mileage may vary).

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

I learned this trick from “Stupid Scanner tricks” article. The reason it works is because Scanner iterates over tokens in the stream, and in this case we separate tokens using “beginning of the input boundary” (\A), thus giving us only one token for the entire contents of the stream.

Note, if you need to be specific about the input stream’s encoding, you can provide the second argument to Scanner constructor that indicates what character set to use (e.g. “UTF-8”).

Hat tip goes also to Jacob, who once pointed me to the said article.

1

  • Shouldn’t we close the scanner before returning the value?

    Oct 19, 2020 at 6:13