Java – Convert InputStream to String

How to convert InputStream to String? There many ways to do so. We will see four ways.

java-logo

Objective:

How to convert InputStream to String?

Environment & Tools:

  • Eclipse
  • JDK 1.6
  • Maven

Libraries:

<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

( 1 ) InputStream to String

  • java.util.Scanner
    return new Scanner(inputStream,"UTF-8").next();
  • java.io.BufferedReader
return new BufferedReader(new InputStreamReader(inputStream)).readLine();
  • org.apache.commons.io.IOUtils
return IOUtils.toString(inputStream);
  • org.apache.commons.io.IOUtils
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
return writer.toString();

Complete Code

InputStreamToString.java

package com.hmkcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Scanner;
import org.apache.commons.io.IOUtils;

public class InputStreamToString {

    // 1. By java.util.Scanner
    public static String getStringByScanner(InputStream inputStream){
    	return new Scanner(inputStream,"UTF-8").next();
    }

    // 2. By BufferedReader
    public static String getStringByBufferedReader(InputStream inputStream) throws IOException{
    	BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    	String line = "";
    	String result = "";
    	while((line = bufferedReader.readLine()) != null)
    		result += line;
    	return result;
    }

    // 3. By Apache IO
    public static String getStringByApacheIO_toString(InputStream inputStream) throws IOException{
    	return IOUtils.toString(inputStream);
    }

    // 4. By Apache IO
    public static String getStringByApacheIO_copy(InputStream inputStream) throws IOException{
    	StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "UTF-8");
        return writer.toString();
    }

}

( 2 ) Test

package com.hmkcode;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class App 
{
    public static void main( String[] args ) throws IOException
    {

        // 1. By java.util.Scanner
    	InputStream inputStream = new ByteArrayInputStream("hmkcode..inputStream to String".getBytes());
    	System.out.println("getStringByScanner: "+InputStreamToString.getStringByScanner(inputStream));

        // 2. By BufferedReader
    	inputStream = new ByteArrayInputStream("hmkcode..inputStream to String".getBytes());
    	System.out.println("getStringByBufferedReader: " +InputStreamToString.getStringByBufferedReader(inputStream));

        // 3. By Apache IO
    	inputStream = new ByteArrayInputStream("hmkcode..inputStream to String".getBytes());
    	System.out.println("getStringByApacheIO IOUtils.toString: "+InputStreamToString.getStringByApacheIO_toString(inputStream));

        // 4. By Apache IO
    	inputStream = new ByteArrayInputStream("hmkcode..inputStream to String".getBytes());
    	System.out.println("getStringByApacheIO IOUtils.copy: "+InputStreamToString.getStringByApacheIO_copy(inputStream));

    }

}

Output:

Source Code @ GitHub