Java Program to Convert String to InputStream Last Updated : 12 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Given a string, the task is to convert the string to InputStream which is shown in the below illustrations. Illustration: Input : String : "Geeks for Geeks" Output : Input Stream : Geeks for Geeks Input : String : "A computer science portal" Output : Input stream : A computer science portal In order to reach the goal, we need to use ByteArrayInputStream. So let us discuss how it's done? We can convert a String to an InputStream object by using the ByteArrayInputStream class. The ByteArrayInputStream is a subclass present in InputStream class. In ByteArrayInputStream there is an internal buffer present that contains bytes that reads from the stream. Approach: Get the bytes of the String.Create a new ByteArrayInputStream using the bytes of the StringAssign the ByteArrayInputStream object to an InputStream variable.Buffer contains bytes that read from the stream.Print the InputStream. Example: Java // Java Program to Convert String to InputStream // Using ByteArrayInputStream // Importing required libraries import java.io.*; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; // Main class public class GFG { // main driver method public static void main(String[] args) throws IOException { // Custom inout string as an input String string = "Geeks for Geeks"; // Printing the above string System.out.print("String : " + string); // Now, using ByteArrayInputStream to // get the bytes of the string, and // converting them to InputStream InputStream stream = new ByteArrayInputStream(string.getBytes (Charset.forName("UTF-8"))); // Creating an object of BufferedReader class to // take input BufferedReader br = new BufferedReader(new InputStreamReader(stream)); // Printing the input stream // using rreadLine() method String str = br.readLine(); System.out.print("\nInput stream : "); // If string is not NULL while (str != null) { // Keep taking input System.out.println(str); str = br.readLine(); } } } OutputString : Geeks for Geeks Input stream : Geeks for Geeks Comment More infoAdvertise with us Next Article Java Program to Convert String to InputStream M manastole01 Follow Improve Article Tags : Java Java Programs Java-String-Programs Practice Tags : Java Similar Reads Java Program to Convert InputStream to String Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read 4 min read Java Program to Convert OutputStream to String OutputStream is an abstract class that is available in the java.io package. As it is an abstract class in order to use its functionality we can use its subclasses. Some subclasses are FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc. And a String is nothing but a sequence of character 2 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read Program to Convert List to Stream in Java The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack class 3 min read Program to convert a Map to a Stream in Java A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Below are various method to convert Map to Stream in Java: Converting complete Map<Key, Value> into Stream: This can be done with the help of Map.entrySet() method which return 4 min read How to Convert a String to a Path in Java? In Java, we can convert a String representation of a file or directory path into a path object using the Paths utility class. It is a part of the java.nio.file package. The Paths class provides a method called get(String file path location) that converts a sequence of strings representing a path int 2 min read Java Program to Read a File to String There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents 8 min read Program to convert Boxed Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case 3 min read Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil 2 min read Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear 6 min read Like