Java Program to Convert InputStream to String

Last Updated : 4 Sep, 2026

An InputStream in Java is used to read data as a stream of bytes from sources such as files, memory, or network connections. Sometimes, this byte-based data needs to be converted into a String for processing or displaying text.

  • InputStreamReader converts bytes into characters using a character encoding.
  • BufferedReader is useful for reading text efficiently, especially when processing data line by line.
  • Scanner provides a simple way to read and process text from an InputStream.

Ways To Convert InputStream To String

1. Using InputStreamReader

InputStreamReader converts bytes from an InputStream into characters using a character encoding. It acts as a bridge between byte streams and character streams.

Java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class InputStreamReaderDemo {

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the file path: ");
        String filePath = sc.nextLine();

        try (InputStreamReader reader
             = new InputStreamReader(
                 new FileInputStream(filePath))) {

            char[] buffer = new char[1024];
            StringBuilder content = new StringBuilder();

            int charactersRead;

            while ((charactersRead = reader.read(buffer))
                   != -1) {
                content.append(buffer, 0, charactersRead);
            }

            String result = content.toString();

            System.out.println("\nFile content:");
            System.out.println(result);
        }
        catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }

        sc.close();
    }
}

Output

InputStreamReader
InputStreamReader Output


Explanation: The InputStreamReader reads characters from the InputStream and stores them in a StringBuilder. The resulting content is then converted into a String and displayed.

2. Using BufferedReader

BufferedReader reads character data efficiently by buffering the input. Its readLine() method makes it convenient to read text one line at a time.

Java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class BufferedReaderDemo{

    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the file path: ");
        String filePath = scanner.nextLine();

        try (BufferedReader reader
             = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)))) {

            StringBuilder content = new StringBuilder();
            String line;

            // Read the file line by line
            while ((line = reader.readLine()) != null){
                content.append(line);
                content.append(System.lineSeparator());
            }

            // Convert the content to String
            String result = content.toString();

            System.out.println("\nFile content:");
            System.out.print(result);
        }
        catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }

        scanner.close();
    }
}

Output

BufferedReaderOutput
BufferedReader Output

Explanation: The program reads the file line by line using BufferedReader and stores the content in a StringBuilder. The complete content is then converted into a String and displayed as the output.

3. Using Scanner

The Scanner class can also read text from an InputStream. A FileInputStream is passed to the Scanner constructor, after which the contents can be read line by line. This approach is simple when the input needs to be processed using the methods provided by Scanner.

Java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class ScannerDemo {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the file path: ");
        String filePath = input.nextLine();

        try (FileInputStream fileInputStream =
                 new FileInputStream(filePath);
             Scanner scanner = new Scanner(fileInputStream)) {

            StringBuilder content = new StringBuilder();

            // Read the file line by line
            while (scanner.hasNextLine()) {
                content.append(scanner.nextLine());

                if (scanner.hasNextLine()) {
                    content.append(System.lineSeparator());
                }
            }

            // Convert the content to String
            String result = content.toString();

            System.out.println("\nFile content:");
            System.out.println(result);

        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }

        input.close();
    }
}

Output

Scanner-output
Scanner Output

Explanation: Scanner reads the contents of the InputStream line by line and adds them to a StringBuilder. The collected content is then converted into a String.

Comment