Convert String to InputStream in Java



To convert a given string into an InputStream, Java provides the ByteArrayInputStream class that is used along with a built-in method named getBytes().

While displaying a long input string, we are sometimes required to process it in smaller units. At that time, converting that string into an InputStream will be helpful.

In this article, we will learn how we can use the ByteArrayInputStream class to convert a string into an InputStream with the help of example programs.

Before discussing that, we need to learn about the InputStream and BufferedReader classes and the getBytes() method first:

InputStream Class

There are two fundamental classes of I/O Stream, namely InputStream class, which is used to take input from sources like keyboard, disk files, etc. The OutputStream class is used to display or write data to the destination.

Here, the term Stream is an abstraction that represents a sequence of data and is used while performing Input and Output operations in Java.

BufferedReader Class

The BufferedReader is a subclass of the Reader class, which is used to read characters from input streams like local files and keyboards. To read a string using BufferedReader, we need to create an instance of the InputStream class and pass it to the constructor of BufferedReader as a parameter.

The getBytes() Method

It is an in-built method of the String class which is used to encode a string into a byte array. It can take an optional argument to specify the character encoding. If we don't pass any charset, it will use the default one.

We will pass this byte array to ByteArrayInputStream as an argument and assign it to the instance of the InputStream class so that we can convert the string into an InputStream.

String to InputStream Conversion in Java

Let's discuss how to use the classes and methods discussed above in Java programs for String to InputStream conversion.

Example 1

The following Java program demonstrates how to convert a string into an InputStream.

import java.io.*;
public class Example1 {
   public static void main(String[] args) throws IOException {
      try {
         // initializing a string
         String inputString = "Hello! this is Tutorials Point!!";
         // converting the string to InputStream
         InputStream streamIn = new ByteArrayInputStream(inputString.getBytes());
         // creating instance of BufferedReader 
         BufferedReader bufrd = new BufferedReader(new InputStreamReader(streamIn));
         // New string to store the original string
         String info = null;
         // loop to print the result
         while ((info = bufrd.readLine()) != null) {
            System.out.println(info);
         }
	  // to handle the exception if occurred 	 
      } catch(Exception exp) { 
         System.out.println(exp);
      } 
   }
}

The output of the above code is as follows:

Hello! this is Tutorials Point!!

Example 2

As mentioned earlier, we can pass an optional argument to specify the character encoding for the byte array. In the following Java program, we will pass the 'UTF-8' as an argument to the getBytes() method.

import java.io.*;
public class Example2 {
   public static void main(String[] args) throws IOException {
      // initializing a string
      String inputString = "Hello! this is Tutorials Point!!";
      // converting the string to InputStream
      InputStream streamIn = new ByteArrayInputStream(inputString.getBytes("UTF-8"));
      // creating instance of BufferedReader 
      BufferedReader bufrd = new BufferedReader(new InputStreamReader(streamIn));
      // New string to store the original string
      String info = null;
      // loop to print the result
      while ((info = bufrd.readLine()) != null) {
         System.out.println(info);
      }
   }
}

The above code produces the following output:

Hello! this is Tutorials Point!!
Updated on: 2025-05-30T17:14:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements