Java Program to Convert OutputStream to String Last Updated : 09 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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 characters, use double quotes to represent it. The java.io.ByteArrayOutputStream.toString() method converts the stream using the character set. Approach 1: Create an object of ByteArrayoutputStream.Create a String variable and initialize it.Use the write method to copy the contents of the string to the object of ByteArrayoutputStream.Print it.Example: Input : String = "Hello World" Output: Hello WorldBelow is the implementation of the above approach: Java // Java program to demonstrate conversion // from outputStream to string import java.io.*; class GFG { // we know that main will throw // IOException so we are ducking it public static void main(String[] args) throws IOException { // declaring ByteArrayOutputStream ByteArrayOutputStream stream = new ByteArrayOutputStream(); // Initializing string String st = "Hello Geek!"; // writing the specified byte to the output stream stream.write(st.getBytes()); // converting stream to byte array // and typecasting into string String finalString = new String(stream.toByteArray()); // printing the final string System.out.println(finalString); } } OutputHello Geek!Approach 2: Create a byte array and store ASCII value of the characters.Create an object of ByteArrayoutputStream.Use write method to copy the content from the byte array to the object.Print it.Example: Input : array = [71, 69, 69, 75] Output: GEEK Below is the implementation of the above approach: Java // Java program to demonstrate conversion // from outputStream to string import java.io.*; class GFG { public static void main(String[] args) throws IOException { // Initializing empty string // and byte array String str = ""; byte[] bs = { 71, 69, 69, 75, 83, 70, 79, 82, 71, 69, 69, 75, 83 }; // create new ByteArrayOutputStream ByteArrayOutputStream stream = new ByteArrayOutputStream(); // write byte array to the output stream stream.write(bs); // converts buffers using default character set // toString is a method for casting into String type str = stream.toString(); // print System.out.println(str); } } OutputGEEKSFORGEEKS Comment More infoAdvertise with us Next Article Java Program to Convert OutputStream to String dadimadhav Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-String-Programs +1 More 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 String to InputStream 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 2 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 Program to convert List of Integer to List of String in Java The Java.util.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 S 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 Program to convert Primitive 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 Java Program to Convert String to Char Stream Without Using Stream Char stream defines the array of characters. In this article, we will learn the different types of methods for converting a String into a char stream in Java without using Stream. Let us see some methods one by one. ExamplesInput: String = HelloGeeksOutput: [H, e, l, l, o, G, e, e, k, s] Input: Stri 4 min read Program to Convert Stream to an Array in Java A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. 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 3 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 Like