Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Convert OutputStream to String

  • Last Updated : 04 Jan, 2021

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:

  1. Create an object of ByteArrayoutputStream.
  2. Create a String variable and initialize it.
  3. Use the write method to copy the contents of the string to the object of ByteArrayoutputStream.
  4. Print it.

Example:

Input : String = "Hello World"
Output: Hello World

Below 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();
  
        // Intializing 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);
    }
}
Output



Hello Geek!

Approach 2:

  1. Create a byte array and store ASCII value of the characters.
  2. Create an object of ByteArrayoutputStream.
  3. Use write method to copy the content from the byte array to the object.
  4. 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
    {
  
        // Intializing 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);
    }
}
Output
GEEKSFORGEEKS

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :