Leer / convertir un InputStream en una cadena

Si tiene un objeto java.io.InputStream , ¿cómo debe procesar ese objeto y producir un String ?


Supongamos que tengo un InputStream que contiene datos de texto y quiero convertirlo a String , por lo que puedo escribirlo en un archivo de registro.

¿Cuál es la forma más fácil de tomar InputStream y convertirlo a String ?

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

Una buena forma de hacerlo es utilizar Apache commons IOUtils para copiar el InputStream en un StringWriter … algo así como

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

o incluso

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

Alternativamente, puedes usar ByteArrayOutputStream si no quieres mezclar tus Streams y Writers

Esta es una forma de usar solo la biblioteca estándar de Java (tenga en cuenta que la secuencia no está cerrada, YMMV).

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

Aprendí este truco del artículo “Trucos de escáner estúpido” . La razón por la que funciona es porque Scanner itera sobre tokens en la secuencia, y en este caso separamos tokens usando “principio del límite de entrada” (\ A), dándonos así solo un token para todo el contenido de la secuencia.

Tenga en cuenta que, si necesita ser específico sobre la encoding de la stream de entrada, puede proporcionar el segundo argumento al constructor de Scanner que indique qué juego de caracteres utilizar (por ejemplo, “UTF-8”).

La punta del sombrero va también para Jacob, quien una vez me señaló el dicho artículo.

EDITADO: Gracias a una sugerencia de Patrick , hizo que la función sea más robusta al manejar un flujo de entrada vacío. Una edición más: nixed try / catch, el modo de Patrick es más lacónico.

Resumir otras respuestas Encontré 11 formas principales de hacer esto (ver abajo). Y escribí algunas pruebas de rendimiento (ver resultados a continuación):

Formas de convertir un InputStream en una cadena:

  1. Usando IOUtils.toString (Apache Utils)

     String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); 
  2. Usando CharStreams (Guava)

     String result = CharStreams.toString(new InputStreamReader( inputStream, Charsets.UTF_8)); 
  3. Usando el Scanner (JDK)

     Scanner s = new Scanner(inputStream).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; 
  4. Usando Stream API (Java 8). Advertencia : esta solución convierte diferentes saltos de línea (como \r\n ) en \n .

     String result = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n")); 
  5. Usando la API Stream paralela (Java 8). Advertencia : esta solución convierte diferentes saltos de línea (como \r\n ) en \n .

     String result = new BufferedReader(new InputStreamReader(inputStream)).lines() .parallel().collect(Collectors.joining("\n")); 
  6. Uso de InputStreamReader y StringBuilder (JDK)

     final int bufferSize = 1024; final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(inputStream, "UTF-8"); for (; ; ) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } return out.toString(); 
  7. Utilizando StringWriter y IOUtils.copy (Apache Commons)

     StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); return writer.toString(); 
  8. Usando ByteArrayOutputStream y inputStream.read (JDK)

     ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } // StandardCharsets.UTF_8.name() > JDK 7 return result.toString("UTF-8"); 
  9. Usando BufferedReader (JDK). Advertencia: esta solución convierte diferentes saltos de línea (como \n\r ) en la propiedad del sistema line.separator (por ejemplo, en Windows para "\ r \ n").

     String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; boolean flag = false; while ((line = reader.readLine()) != null) { result.append(flag? newLine: "").append(line); flag = true; } return result.toString(); 
  10. Uso de BufferedInputStream y ByteArrayOutputStream (JDK)

     BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { buf.write((byte) result); result = bis.read(); } // StandardCharsets.UTF_8.name() > JDK 7 return buf.toString("UTF-8"); 
  11. Usando inputStream.read() y StringBuilder (JDK). Advertencia : esta solución tiene problemas con Unicode, por ejemplo, con texto en ruso (funciona correctamente solo con texto que no es Unicode)

     int ch; StringBuilder sb = new StringBuilder(); while((ch = inputStream.read()) != -1) sb.append((char)ch); reset(); return sb.toString(); 

Advertencia :

  1. Las soluciones 4, 5 y 9 convierten diferentes saltos de línea en uno.

  2. La solución 11 no puede funcionar correctamente con texto Unicode

Pruebas de rendimiento

Pruebas de rendimiento para String pequeño (longitud = 175), url en github (modo = Tiempo promedio, sistema = Linux, puntaje 1,343 es el mejor):

  Benchmark Mode Cnt Score Error Units 8. ByteArrayOutputStream and read (JDK) avgt 10 1,343 ± 0,028 us/op 6. InputStreamReader and StringBuilder (JDK) avgt 10 6,980 ± 0,404 us/op 10. BufferedInputStream, ByteArrayOutputStream avgt 10 7,437 ± 0,735 us/op 11. InputStream.read() and StringBuilder (JDK) avgt 10 8,977 ± 0,328 us/op 7. StringWriter and IOUtils.copy (Apache) avgt 10 10,613 ± 0,599 us/op 1. IOUtils.toString (Apache Utils) avgt 10 10,605 ± 0,527 us/op 3. Scanner (JDK) avgt 10 12,083 ± 0,293 us/op 2. CharStreams (guava) avgt 10 12,999 ± 0,514 us/op 4. Stream Api (Java 8) avgt 10 15,811 ± 0,605 us/op 9. BufferedReader (JDK) avgt 10 16,038 ± 0,711 us/op 5. parallel Stream Api (Java 8) avgt 10 21,544 ± 0,583 us/op 

Pruebas de rendimiento para String grande (longitud = 50100), url en github (modo = Tiempo promedio, sistema = Linux, puntaje 200,715 es el mejor):

  Benchmark Mode Cnt Score Error Units 8. ByteArrayOutputStream and read (JDK) avgt 10 200,715 ± 18,103 us/op 1. IOUtils.toString (Apache Utils) avgt 10 300,019 ± 8,751 us/op 6. InputStreamReader and StringBuilder (JDK) avgt 10 347,616 ± 130,348 us/op 7. StringWriter and IOUtils.copy (Apache) avgt 10 352,791 ± 105,337 us/op 2. CharStreams (guava) avgt 10 420,137 ± 59,877 us/op 9. BufferedReader (JDK) avgt 10 632,028 ± 17,002 us/op 5. parallel Stream Api (Java 8) avgt 10 662,999 ± 46,199 us/op 4. Stream Api (Java 8) avgt 10 701,269 ± 82,296 us/op 10. BufferedInputStream, ByteArrayOutputStream avgt 10 740,837 ± 5,613 us/op 3. Scanner (JDK) avgt 10 751,417 ± 62,026 us/op 11. InputStream.read() and StringBuilder (JDK) avgt 10 2919,350 ± 1101,942 us/op 

Gráficos (pruebas de rendimiento según la longitud del flujo de entrada en el sistema Windows 7)
enter image description here

Prueba de rendimiento (tiempo promedio) según la longitud del flujo de entrada en el sistema Windows 7:

  length 182 546 1092 3276 9828 29484 58968 test8 0.38 0.938 1.868 4.448 13.412 36.459 72.708 test4 2.362 3.609 5.573 12.769 40.74 81.415 159.864 test5 3.881 5.075 6.904 14.123 50.258 129.937 166.162 test9 2.237 3.493 5.422 11.977 45.98 89.336 177.39 test6 1.261 2.12 4.38 10.698 31.821 86.106 186.636 test7 1.601 2.391 3.646 8.367 38.196 110.221 211.016 test1 1.529 2.381 3.527 8.411 40.551 105.16 212.573 test3 3.035 3.934 8.606 20.858 61.571 118.744 235.428 test2 3.136 6.238 10.508 33.48 43.532 118.044 239.481 test10 1.593 4.736 7.527 20.557 59.856 162.907 323.147 test11 3.913 11.506 23.26 68.644 207.591 600.444 1211.545 

Apache Commons permite:

 String myString = IOUtils.toString(myInputStream, "UTF-8"); 

Por supuesto, puedes elegir otras codificaciones de caracteres además de UTF-8.

Ver también: ( Docs )

Teniendo en cuenta el archivo uno primero debe obtener una instancia de java.io.Reader . Esto luego puede leerse y agregarse a un StringBuilder (no necesitamos StringBuffer si no estamos accediendo a él en múltiples hilos, y StringBuilder es más rápido). El truco aquí es que trabajamos en bloques, y como tal no necesitamos otras secuencias de almacenamiento en búfer. El tamaño del bloque está parametrizado para la optimización del rendimiento en tiempo de ejecución.

 public static String slurp(final InputStream is, final int bufferSize) { final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); try (Reader in = new InputStreamReader(is, "UTF-8")) { for (;;) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } } catch (UnsupportedEncodingException ex) { /* ... */ } catch (IOException ex) { /* ... */ } return out.toString(); } 

¿Qué tal esto?

 InputStream in = /* your InputStream */ ; StringBuilder sb=new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String read; while((read=br.readLine()) != null) { //System.out.println(read); sb.append(read); } br.close(); return sb.toString(); 

Si está utilizando Google-Collections / Guava, puede hacer lo siguiente:

 InputStream stream = ... String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); Closeables.closeQuietly(stream); 

Tenga en cuenta que el segundo parámetro (es decir, Charsets.UTF_8) para el InputStreamReader no es necesario, pero generalmente es una buena idea especificar la encoding si la conoce (¡lo que debería hacer!)

Esta es mi solución pura de Java y Android, funciona bien …

 public String readFullyAsString(InputStream inputStream, String encoding) throws IOException { return readFully(inputStream).toString(encoding); } public byte[] readFullyAsBytes(InputStream inputStream) throws IOException { return readFully(inputStream).toByteArray(); } private ByteArrayOutputStream readFully(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) != -1) { baos.write(buffer, 0, length); } return baos; } 

Qué tal si:

 import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; public static String readInputStreamAsString(InputStream in) throws IOException { BufferedInputStream bis = new BufferedInputStream(in); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { byte b = (byte)result; buf.write(b); result = bis.read(); } return buf.toString(); } 

Aquí está la solución más elegante, puramente Java (sin biblioteca) que surgió después de algunos experimentos:

 public static String fromStream(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String newLine = System.getProperty("line.separator"); String line; while ((line = reader.readLine()) != null) { out.append(line); out.append(newLine); } return out.toString(); } 

Para completar, aquí está la solución de Java 9 :

 public static String toString(InputStream input) throws IOException { return new String(input.readAllBytes(), StandardCharsets.UTF_8); } 

El readAllBytes encuentra actualmente en la base de código principal JDK 9, por lo que es probable que aparezca en el lanzamiento. Puede intentarlo ahora usando las comstackciones de instantáneas JDK 9 .

Usaría algunos trucos de Java 8.

 public static String streamToString(final InputStream inputStream) throws Exception { // buffering optional try ( final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)) ) { // parallel optional return br.lines().parallel().collect(Collectors.joining("\n")); } catch (final IOException e) { throw new RuntimeException(e); // whatever. } } 

Esencialmente lo mismo que algunas otras respuestas excepto más sucinto.

Realicé algunas pruebas de tiempo porque el tiempo importa, siempre.

Intenté obtener la respuesta en un String 3 de diferentes maneras. (mostrado a continuación)
Olvidé los bloques try / catch para la lectura del sake.

Para dar contexto, este es el código anterior para los 3 enfoques:

  String response; String url = "www.blah.com/path?key=value"; GetMethod method = new GetMethod(url); int status = client.executeMethod(method); 

1)

  response = method.getResponseBodyAsString(); 

2)

 InputStream resp = method.getResponseBodyAsStream(); InputStreamReader is=new InputStreamReader(resp); BufferedReader br=new BufferedReader(is); String read = null; StringBuffer sb = new StringBuffer(); while((read = br.readLine()) != null) { sb.append(read); } response = sb.toString(); 

3)

 InputStream iStream = method.getResponseBodyAsStream(); StringWriter writer = new StringWriter(); IOUtils.copy(iStream, writer, "UTF-8"); response = writer.toString(); 

Entonces, después de ejecutar 500 pruebas en cada enfoque con los mismos datos de solicitud / respuesta, aquí están los números. Una vez más, estos son mis hallazgos y sus conclusiones pueden no ser exactamente las mismas, pero escribí esto para dar alguna indicación a otros de las diferencias de eficiencia de estos enfoques.

Rangos:
Enfoque # 1
Enfoque n. ° 3: 2.6% más lento que n. ° 1
Enfoque n. ° 2: 4.3% más lento que n. ° 1

Cualquiera de estos enfoques es una solución adecuada para tomar una respuesta y crear una cadena a partir de ella.

La solución Pure Java que utiliza Stream s funciona desde Java 8.

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.stream.Collectors; // ... public static String inputStreamToString(InputStream is) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { return br.lines().collect(Collectors.joining(System.lineSeparator())); } } 

Como lo menciona Christoffer Hammarström debajo de otra respuesta , es más seguro especificar explícitamente el Charset . Es decir, el constructor de InputStreamReader se puede cambiar de la siguiente manera:

 new InputStreamReader(is, Charset.forName("UTF-8")) 

Hice un punto de referencia sobre 14 respuestas distintas aquí (lo siento por no proporcionar créditos, pero hay demasiados duplicados)

El resultado es muy sorprendente. Resulta que Apache IOUtils es el más lento y ByteArrayOutputStream es la solución más rápida:

Entonces, primero aquí está el mejor método:

 public String inputStreamToString(InputStream inputStream) throws IOException { try(ByteArrayOutputStream result = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString(UTF_8); } } 

Resultados de referencia, de 20 MB de bytes aleatorios en 20 ciclos

Tiempo en milisegundos

  • ByteArrayOutputStreamTest: 194
  • NioStream: 198
  • Java9ISTransferTo: 201
  • Java9ISReadAllBytes: 205
  • BufferedInputStreamVsByteArrayOutputStream: 314
  • ApacheStringWriter2: 574
  • GuavaCharStreams: 589
  • ScannerReaderNoNextTest: 614
  • ScannerReader: 633
  • ApacheStringWriter: 1544
  • StreamApi: error
  • ParallelStreamApi: error
  • BufferReaderTest: error
  • InputStreamAndStringBuilder: error

Código fuente de referencia

 import com.google.common.io.CharStreams; import org.apache.commons.io.IOUtils; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.stream.Collectors; /** * Created by Ilya Gazman on 2/13/18. */ public class InputStreamToString { private static final String UTF_8 = "UTF-8"; public static void main(String... args) { log("App started"); byte[] bytes = new byte[1024 * 1024]; new Random().nextBytes(bytes); log("Stream is ready\n"); try { test(bytes); } catch (IOException e) { e.printStackTrace(); } } private static void test(byte[] bytes) throws IOException { List tests = Arrays.asList( new ApacheStringWriter(), new ApacheStringWriter2(), new NioStream(), new ScannerReader(), new ScannerReaderNoNextTest(), new GuavaCharStreams(), new StreamApi(), new ParallelStreamApi(), new ByteArrayOutputStreamTest(), new BufferReaderTest(), new BufferedInputStreamVsByteArrayOutputStream(), new InputStreamAndStringBuilder(), new Java9ISTransferTo(), new Java9ISReadAllBytes() ); String solution = new String(bytes, "UTF-8"); for (Stringify test : tests) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { String s = test.inputStreamToString(inputStream); if (!s.equals(solution)) { log(test.name() + ": Error"); continue; } } long startTime = System.currentTimeMillis(); for (int i = 0; i < 20; i++) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { test.inputStreamToString(inputStream); } } log(test.name() + ": " + (System.currentTimeMillis() - startTime)); } } private static void log(String message) { System.out.println(message); } interface Stringify { String inputStreamToString(InputStream inputStream) throws IOException; default String name() { return this.getClass().getSimpleName(); } } static class ApacheStringWriter implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, UTF_8); return writer.toString(); } } static class ApacheStringWriter2 implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return IOUtils.toString(inputStream, UTF_8); } } static class NioStream implements Stringify { @Override public String inputStreamToString(InputStream in) throws IOException { ReadableByteChannel channel = Channels.newChannel(in); ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 16); ByteArrayOutputStream bout = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(bout); while (channel.read(byteBuffer) > 0 || byteBuffer.position() > 0) { byteBuffer.flip(); //make buffer ready for write outChannel.write(byteBuffer); byteBuffer.compact(); //make buffer ready for reading } channel.close(); outChannel.close(); return bout.toString(UTF_8); } } static class ScannerReader implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } } static class ScannerReaderNoNextTest implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.next(); } } static class GuavaCharStreams implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { return CharStreams.toString(new InputStreamReader( is, UTF_8)); } } static class StreamApi implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n")); } } static class ParallelStreamApi implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new BufferedReader(new InputStreamReader(inputStream)).lines() .parallel().collect(Collectors.joining("\n")); } } static class ByteArrayOutputStreamTest implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { try(ByteArrayOutputStream result = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString(UTF_8); } } } static class BufferReaderTest implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(UTF_8); String line; boolean flag = false; while ((line = reader.readLine()) != null) { result.append(flag ? newLine : "").append(line); flag = true; } return result.toString(); } } static class BufferedInputStreamVsByteArrayOutputStream implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while (result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString(UTF_8); } } static class InputStreamAndStringBuilder implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { int ch; StringBuilder sb = new StringBuilder(UTF_8); while ((ch = inputStream.read()) != -1) sb.append((char) ch); return sb.toString(); } } static class Java9ISTransferTo implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); inputStream.transferTo(bos); return bos.toString(UTF_8); } } static class Java9ISReadAllBytes implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new String(inputStream.readAllBytes(), UTF_8); } } } 

Here’s more-or-less sampath’s answer, cleaned up a bit and represented as a function:

 String streamToString(InputStream in) throws IOException { StringBuilder out = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); for(String line = br.readLine(); line != null; line = br.readLine()) out.append(line); br.close(); return out.toString(); } 

If you were feeling adventurous, you could mix Scala and Java and end up with this:

 scala.io.Source.fromInputStream(is).mkString("") 

Mixing Java and Scala code and libraries has it’s benefits.

See full description here: Idiomatic way to convert an InputStream to a String in Scala

If you can’t use Commons IO (FileUtils/IOUtils/CopyUtils) here’s an example using a BufferedReader to read the file line by line:

 public class StringFromFile { public static void main(String[] args) /*throws UnsupportedEncodingException*/ { InputStream is = StringFromFile.class.getResourceAsStream("file.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is/*, "UTF-8"*/)); final int CHARS_PER_PAGE = 5000; //counting spaces StringBuilder builder = new StringBuilder(CHARS_PER_PAGE); try { for(String line=br.readLine(); line!=null; line=br.readLine()) { builder.append(line); builder.append('\n'); } } catch (IOException ignore) { } String text = builder.toString(); System.out.println(text); } } 

or if you want raw speed I’d propose a variation on what Paul de Vrieze suggested (which avoids using a StringWriter (which uses a StringBuffer internally) :

 public class StringFromFileFast { public static void main(String[] args) /*throws UnsupportedEncodingException*/ { InputStream is = StringFromFileFast.class.getResourceAsStream("file.txt"); InputStreamReader input = new InputStreamReader(is/*, "UTF-8"*/); final int CHARS_PER_PAGE = 5000; //counting spaces final char[] buffer = new char[CHARS_PER_PAGE]; StringBuilder output = new StringBuilder(CHARS_PER_PAGE); try { for(int read = input.read(buffer, 0, buffer.length); read != -1; read = input.read(buffer, 0, buffer.length)) { output.append(buffer, 0, read); } } catch (IOException ignore) { } String text = output.toString(); System.out.println(text); } } 

This is an answer adapted from org.apache.commons.io.IOUtils source code , for those who want to have the apache implementation but do not want the whole library.

 private static final int BUFFER_SIZE = 4 * 1024; public static String inputStreamToString(InputStream inputStream, String charsetName) throws IOException { StringBuilder builder = new StringBuilder(); InputStreamReader reader = new InputStreamReader(inputStream, charsetName); char[] buffer = new char[BUFFER_SIZE]; int length; while ((length = reader.read(buffer)) != -1) { builder.append(buffer, 0, length); } return builder.toString(); } 

Make sure to close the streams at end if you use Stream Readers

 private String readStream(InputStream iStream) throws IOException { //build a Stream Reader, it can read char by char InputStreamReader iStreamReader = new InputStreamReader(iStream); //build a buffered Reader, so that i can read whole line at once BufferedReader bReader = new BufferedReader(iStreamReader); String line = null; StringBuilder builder = new StringBuilder(); while((line = bReader.readLine()) != null) { //Read till end builder.append(line); builder.append("\n"); // append new line to preserve lines } bReader.close(); //close all opened stuff iStreamReader.close(); //iStream.close(); //EDIT: Let the creator of the stream close it! // some readers may auto close the inner stream return builder.toString(); } 

EDIT: On JDK 7+, you can use try-with-resources construct.

 /** * Reads the stream into a string * @param iStream the input stream * @return the string read from the stream * @throws IOException when an IO error occurs */ private String readStream(InputStream iStream) throws IOException { //Buffered reader allows us to read line by line try (BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream))){ StringBuilder builder = new StringBuilder(); String line; while((line = bReader.readLine()) != null) { //Read till end builder.append(line); builder.append("\n"); // append new line to preserve lines } return builder.toString(); } } 

Here is the complete method for converting InputStream into String without using any third party library. Use StringBuilder for single threaded environment otherwise use StringBuffer .

 public static String getString( InputStream is) throws IOException { int ch; StringBuilder sb = new StringBuilder(); while((ch = is.read()) != -1) sb.append((char)ch); return sb.toString(); } 

Here’s how to do it using just the JDK using byte array buffers. This is actually how the commons-io IOUtils.copy() methods all work. You can replace byte[] with char[] if you’re copying from a Reader instead of an InputStream .

 import java.io.ByteArrayOutputStream; import java.io.InputStream; ... InputStream is = .... ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); byte[] buffer = new byte[8192]; int count = 0; try { while ((count = is.read(buffer)) != -1) { baos.write(buffer, 0, count); } } finally { try { is.close(); } catch (Exception ignore) { } } String charset = "UTF-8"; String inputStreamAsString = baos.toString(charset); 

Kotlin users simply do:

 println(InputStreamReader(is).readText()) 

mientras

 readText() 

is Kotlin standard library’s built-in extension method.

Another one, for all the Spring users:

 import java.nio.charset.StandardCharsets; import org.springframework.util.FileCopyUtils; public String convertStreamToString(InputStream is) throws IOException { return new String(FileCopyUtils.copyToByteArray(is), StandardCharsets.UTF_8); } 

The utility methods in org.springframework.util.StreamUtils are similar to the ones in FileCopyUtils , but they leave the stream open when done.

Use the java.io.InputStream.transferTo(OutputStream) supported in Java 9 and the ByteArrayOutputStream.toString(String) which takes the charset name:

 public static String gobble(InputStream in, String charsetName) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); in.transferTo(bos); return bos.toString(charsetName); } 

This one is nice because:

  • Hand safety the Charset.
  • You control the read buffer size.
  • You can provision the length of the builder and can be not exactly.
  • Is free from library dependencies.
  • Is for Java 7 or higher.

What the for?

 public static String convertStreamToString(InputStream is) { if (is == null) return null; StringBuilder sb = new StringBuilder(2048); // Define a size if you have an idea of it. char[] read = new char[128]; // Your buffer size. try (InputStreamReader ir = new InputStreamReader(is, StandardCharsets.UTF_8)) { for (int i; -1 != (i = ir.read(read)); sb.append(read, 0, i)); } catch (Throwable t) {} return sb.toString(); } 

Here’s my Java 8 based solution, which uses the new Stream API to collect all lines from an InputStream :

 public static String toString(InputStream inputStream) { BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); return reader.lines().collect(Collectors.joining( System.getProperty("line.separator"))); } 

The easiest way in JDK is with the following code snipplets.

 String convertToString(InputStream in){ String resource = new Scanner(in).useDelimiter("\\Z").next(); return resource; } 

Guava provides much shorter efficient autoclosing solution in case when input stream comes from classpath resource (which seems to be popular task):

 byte[] bytes = Resources.toByteArray(classLoader.getResource(path)); 

o

 String text = Resources.toString(classLoader.getResource(path), StandardCharsets.UTF_8); 

There is also general concept of ByteSource and CharSource that gently take care of both opening and closing the stream.

So, for example, instead of explicitly opening a small file to read its contents:

 String content = Files.asCharSource(new File("robots.txt"), StandardCharsets.UTF_8).read(); byte[] data = Files.asByteSource(new File("favicon.ico")).read(); 

o solo

 String content = Files.toString(new File("robots.txt"), StandardCharsets.UTF_8); byte[] data = Files.toByteArray(new File("favicon.ico")); 

Note: This probably isn’t a good idea. This method uses recursion:

 public String read (InputStream is) { byte next = is.read(); return next == -1 ? "" : next + read(is); // Recursive part: reads next byte recursively } 

Please don’t downvote this just because it’s a bad choice to use; this was mostly creative 🙂