How do I read / convert an InputStream into a String in Java?
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
How do I read / convert an InputStream into a String in Java?
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
How do I convert an InputStream to a String in Java?
This is my version,
public static String readString(InputStream inputStream) throws IOException {
ByteArrayOutputStream into = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int n; 0 < (n = inputStream.read(buf));) {
into.write(buf, 0, n);
}
into.close();
return new String(into.toByteArray(), "UTF-8"); // Or whatever encoding
}
Java String from InputStream
Here is a modification of Gopi's answer that doesn't have the line ending problem and is also more effective as it doesn't need temporary String objects for every line and avoids the redundant copying in BufferedReader and the extra work in readLine().
public static String convertStreamToString( InputStream is, String ecoding ) throws IOException
{
StringBuilder sb = new StringBuilder( Math.max( 16, is.available() ) );
char[] tmp = new char[ 4096 ];
try {
InputStreamReader reader = new InputStreamReader( is, ecoding );
for( int cnt; ( cnt = reader.read( tmp ) ) > 0; )
sb.append( tmp, 0, cnt );
} finally {
is.close();
}
return sb.toString();
}
Convert InputStream into StreamString given a Charset
I think you can try:
Stream<String> lines = new BufferedReader(new InputStreamReader(is, cs)).lines();
Convert Contents Of A ByteArrayInputStream To String
A ByteArrayOutputStream
can read from any InputStream
and at the end yield a byte[]
.
However with a ByteArrayInputStream
it is simpler:
int n = in.available();
byte[] bytes = new byte[n];
in.read(bytes, 0, n);
String s = new String(bytes, StandardCharsets.UTF_8); // Or any encoding.
For a ByteArrayInputStream
available()
yields the total number of bytes.
Addendum 2021-11-16
Since java 9 you can use the shorter readAllBytes.
byte[] bytes = in.readAllBytes();
Answer to comment: using ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
for (;;) {
int nread = in.read(buf, 0, buf.length);
if (nread <= 0) {
break;
}
baos.write(buf, 0, nread);
}
in.close();
baos.close();
byte[] bytes = baos.toByteArray();
Here in may be any InputStream.
Since java 10 there also is a ByteArrayOutputStream#toString(Charset)
.
String s = baos.toString(StandardCharsets.UTF_8);
Related Topics
Java Command Not Found on Linux
Java_Home and Path Are Set But Java -Version Still Shows the Old One
Where to Store Application Data (Non-User Specific) on Linux
What Is a Nullpointerexception, and How to Fix It
Scanner Is Skipping Nextline() After Using Next() or Nextfoo()
How to Write a Correct Micro-Benchmark in Java
How to Add Jtable in Jpanel With Null Layout
What's the Simplest Way to Print a Java Array
What Is a Stack Trace, and How to Use It to Debug My Application Errors
How to Upload Files to a Server Using Jsp/Servlet
How to Generate Random Integers Within a Specific Range in Java
What Issues Should Be Considered When Overriding Equals and Hashcode in Java
Why Am I Getting a Noclassdeffounderror in Java
How to Create an Executable Jar With Dependencies Using Maven
Why Use Getters and Setters/Accessors
Sort a Map≪Key, Value≫ by Values
What Is the Java String Pool and How Is "S" Different from New String("S")