Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Convert a String into the InputStream


In this article, we will understand how to convert a string into the inputStream. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). InputStream class is the superclass of all classes representing an input stream of bytes.

Below is a demonstration of the same −

Suppose our input is

Input string: Java Program

The desired output would be

The number of bytes available at the beginning: 12
The number of bytes available at the end: 10

Algorithm

Step 1 - START
Step 2 - Declare string namely input_string, an object of InputStream namely input_stream.
Step 3 - Define the values.
Step 4 - Use the function read() to read the bytes and .available() to fetch the available bytes.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Demo {
   public static void main(String args[]) {
      String input_string = "Java Program";
      System.out.println("The string is defined as: " + input_string);
      try {
         InputStream input_stream = new ByteArrayInputStream(input_string.getBytes(StandardCharsets.UTF_8));
         System.out.println("The number of bytes available at the beginning: " + input_stream.available());
         input_stream.read();
         input_stream.read();
         System.out.println("The number of bytes available at the end: " + input_stream.available());
         input_stream.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output

The string is defined as: Java Program
The number of bytes available at the beginning: 12
The number of bytes available at the end: 10

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Demo {
   static void check_bytes(String input_string){
      try {
         InputStream input_stream = new ByteArrayInputStream(input_string.getBytes(StandardCharsets.UTF_8));
         System.out.println("The number of bytes available at the beginning: " + input_stream.available());
         input_stream.read();
         input_stream.read();
         System.out.println("The number of bytes available at the end: " + input_stream.available());
         input_stream.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
   public static void main(String args[]) {
      String input_string = "Java Program";
      System.out.println("The string is defined as: " + input_string);
      check_bytes(input_string);
   }
}

Output

The string is defined as: Java Program
The number of bytes available at the beginning: 12
The number of bytes available at the end: 10