Java Program to Convert String to InputStream Last Updated : 12 Jun, 2021 Comments Improve Suggest changes 3 Likes 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 Create Quiz Comment M manastole01 Follow 3 Improve M manastole01 Follow 3 Improve Article Tags : Java Java Programs Java-String-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like