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

Technology CommunityCategory: JavaHow do I read/convert an InputStream into a String in Java?
VietMX Staff asked 3 years ago

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

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

or

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