How to convert InputStream object to a String in Java?


Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.

There are two types of streams available −

  • InputStream − This is used to read (sequential) data from a source.
  • OutputStream − This is used to write data to a destination.

FileInputStream

This class reads the data from a specific file (byte by byte). It is usually used to read the contents of a file with raw bytes, such as images.

Converting an InputStream object to String

You can convert an InputStream Object int to a String in several ways using core Java. You can also use external libraries like IOUtils, Guava for this purpose. Following are some ways to convert an InputStream object to String in Java (not including external libraries).

Using BufferedReader

The readLine() method of the BufferedReader class reads a single line from the contents of the current reader. To convert an InputStream Object int to a String using this method.

  • Instantiate an InputStreamReader class by passing your InputStream object as parameter.
  • Then, create a BufferedReader, by passing above obtained InputStreamReader object as a parameter.
  • Now, read each line from this reader using the readLine() method and append it to a StringBuffer object.
  • Finally convert the StringBuffer to String using the toString() method.

Example

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class InputStreamToString{
   public static void main(String args[]) throws IOException {
      //Creating an InputStream object
      InputStream inputStream = new FileInputStream("D:/sample.txt");
      //creating an InputStreamReader object
      InputStreamReader isReader = new InputStreamReader(inputStream);
      //Creating a BufferedReader object
      BufferedReader reader = new BufferedReader(isReader);
      StringBuffer sb = new StringBuffer();
      String str;
      while((str = reader.readLine())!= null){
         sb.append(str);
      }
      System.out.println(sb.toString());
   }
}

Output

Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line
content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

Using the Scanner class

The nextLine() method of the Scanner class reads the contents of the underlying inputStream line by line. To convert an InputStream Object int to a String using this method.

  • Instantiate the Scanner class by passing your InputStream object as parameter.
  • Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object.
  • Finally convert the StringBuffer to String using the toString() method.

Example

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class InputStreamToString {
   public static void main(String args[]) throws IOException {
      //Creating an InputStream object
      InputStream inputStream = new FileInputStream("D:/sample.txt");
      //Creating a Scanner object
      Scanner sc = new Scanner(inputStream);
      //Reading line by line from scanner to StringBuffer
      StringBuffer sb = new StringBuffer();
      while(sc.hasNext()){
         sb.append(sc.nextLine());
      }
      System.out.println(sb.toString());
   }
}

Output

Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line
content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

Using the InputStreamReader class

The read() method of the InputStreamReader class accepts a character array as a parameter and reads the contents of the current Stream to the given array. To convert an InputStream Object int to a String using this method.

  • Instantiate an InputStreamReader class by passing your InputStream object as parameter.
  • Read the contents of the current stream reader to a character array using the read() method of the InputStreamReader class.
  • Finally convert the character to a String by passing it as a parameter to its constructor.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileInputStreamExample {
   public static void main(String args[]) throws IOException {
      //Creating a File object
      File file = new File("D:/sample.txt");
      //Creating an InputStream object
      InputStream inputStream = new FileInputStream(file);
      //creating an InputStreamReader object
      InputStreamReader isReader = new InputStreamReader(inputStream);
      //Creating a character array
      char charArray[] = new char[(int) file.length()];
      //Reading the contents of the reader
      isReader.read(charArray);
      //Converting character array to a String
      String contents = new String(charArray);
      System.out.println(contents);
   }
}

Output

Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line 
content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

Updated on: 01-Aug-2019

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements