
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to InputStream in Java
To convert a given string into the InputStream, Java provides ByteArrayInputStream class that is used along with a built method named getBytes(). 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.
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 a few concepts first ?
InputStream
There are two fundamental classes of stream namely InputStream class, which is used to take input from sources like keyboard, disk files etc. And, the OutputStream class, which is used to display or write data to the destination. Here, the term Stream is an abstraction that is used while performing Input and Output operations in Java.
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.
The 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.
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.
String to InputStream Conversion in Java
Let's discuss a few Java programs of String to InputStream conversion ?
Example 1
The following Java program demonstrates how to convert a string into the 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); } } catch(Exception exp) { // to handle the exception if occurred 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 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 following 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.