Java Program to Convert a String into the InputStream


While displaying a long input string, we are sometimes required to process them in smaller units. At that time, converting that string into the InputStream will be helpful. To convert a given string into the InputStream, Java provides ByteArrayInputStream class that is used along with a built method named getBytes(). 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.

Java Program to Convert a String into the InputStream

This section will introduce a few concepts that will help us to understand the Java programs for string to InputStream conversion.

InputStream

Stream is an abstraction that is used while performing Input and Output operations in Java. There are two fundamental terms of stream namely InputStream class which is used to take input from sources like keyboard, disk files etc. And, the OutputStream class which is refer to the destination where data gets displayed or written.

BufferedReader Class

Since InputStream class is an abstract class, we are required to use its subclasses to implement its functionalities. One of the subclass is BufferedReader 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.

Syntax

BufferedReader nameOfObject = new BufferedReader(objectOfStream);

getBytes() method

It is an in-built method of 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.

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

Example 1

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

Approach

  • First, initialize a string.

  • Then, use the getBytes() method along with the ByteArrayInputStream class to convert the specified string into a byte array with the default charset and assign that array to the InputStream.

  • Next, use the BufferedReader class to read the stream.

  • In the end, use a while loop that will run till the content of stream is not equal to 'null' and print the stream.

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);
         }
      } catch(Exception exp) { // to handle the exception if occurred
         System.out.println(exp);
      } 
   }
}

Output

Hello! this is Tutorials Point!!

Example 2

As mentioned earlier, we can pass an optional argument to specify the character encoding for 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);
      }
   }
}

Output

Hello! this is Tutorials Point!!

Conclusion

In this article, we have learned how to convert a given string into the InputStream in Java. For this purpose, we discussed two Java programs that utilize the ByteArrayInputStream class, BufferedReader class and getBytes() method. Using the getBytes() method and ByteArrayInputStream, we were able to convert the string into an integer and with the help of the BufferedReader class, we read that stream.

Updated on: 10-Aug-2023

482 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements