How do I read and convert an InputStream object to string type?

0 votes

If you have 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 this to a String. For example, so I can write the contents of the stream to a log file.

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

public String convertStreamToString(InputStream is) { 
    // ???
Apr 17, 2018 in Java by parth295
• 4,600 points

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Ways to convert an InputStream to a String:

  1. Using IOUtils.toString (Apache Utils)

    String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  2. Using CharStreams (guava)

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

    Scanner s = new Scanner(inputStream).useDelimiter("\\A");
    String result = s.hasNext() ? : "";
  4. Using Stream Api (Java 8). Warning: This solution convert different line breaks (like \r\n) to \n.

    String result = new BufferedReader(new InputStreamReader(inputStream))
  5. Using parallel Stream Api (Java 8). Warning: This solution convert different line breaks (like \r\n) to \n.

    String result = new BufferedReader(new InputStreamReader(inputStream)).lines()
  6. Using InputStreamReader and 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 =, 0, buffer.length);
        if (rsz < 0)
        out.append(buffer, 0, rsz);
    return out.toString();
  7. Using StringWriter and IOUtils.copy (Apache Commons)

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

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = != -1) {
        result.write(buffer, 0, length);
    // > JDK 7
    return result.toString("UTF-8");
  9. Using BufferedReader (JDK). Warning: This solution convert different line breaks (like \n\r) to line.separator system property (for example, in Windows to "\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. Using BufferedInputStream and ByteArrayOutputStream (JDK)

    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result =;
    while(result != -1) {
        buf.write((byte) result);
        result =;
    // > JDK 7
    return buf.toString("UTF-8");
  11. Using and StringBuilder (JDK). Warning: This solution has problem with Unicode, for example with Russian text (work correctly only with non-Unicode text)

    int ch;
    StringBuilder sb = new StringBuilder();
    while((ch = != -1)
    return sb.toString();


  1. Solutions 4, 5 and 9 convert different line breaks to one.

  2. Solution 11 can't work correctly with Unicode text

answered Apr 17, 2018 by Rishabh
• 3,520 points
0 votes

You can also use Java Standard Library to read and convert object into String type.

static String convertStreamToString( is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? : "";
answered Jul 17, 2018 by Sushmita
• 6,840 points

Related Questions In Java

0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
+14 votes
17 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7 in Java by Suresh
• 700 points
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How do I convert an input stream into a byte array

There is a simple solution without the ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,280 points
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,520 points
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,840 points
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,520 points
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,520 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.