Convert OutputStream to String in Java

Sheeraz Gul Sep 01, 2022
Convert OutputStream to String in Java

This tutorial demonstrates how to convert OutputStream to String in Java.

Convert OutputStream to String in Java

OutputStream is an abstract class of io package of java which includes subclasses like FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc.

On the other hand, String is a simple set of characters. Therefore, converting OutputStream to a String is easy. To do that, we can use the java.io.ByteArrayOutputStream.toString() method.

Or we can cast an OutputStream to a String. Let’s write code examples to convert OutputStream to String.

Example One:

This code includes the five steps. First, initialize an empty String. Second, Create an array with ASCII values of your String. In the third step, create an object of OutputStream.

Fourth, use the write method to copy the Byte array to the object. Finally, print the final strings.

package delftstack;

import java.io.*;

class Example1 {
    public static void main(String[] args) throws IOException {

    	// Initialize empty string and byte array
        String DemoString = "";
        byte[] ByteArray = { 72, 101, 108, 108, 111, 33, 32, 
        		84, 104, 105, 115, 32, 105, 115, 32,
        		100, 101, 108, 102, 116, 115, 116, 97,
        		99, 107, 46, 99, 111, 109, 46, 46 };

        // create ByteArrayOutputStream
        ByteArrayOutputStream Output_Stream = new ByteArrayOutputStream();

        // Now write byte array to the output stream
        Output_Stream.write(ByteArray);

        // convert buffers using the toString method into String type
        DemoString = Output_Stream.toString();

        // print The String
        System.out.println("The converted String is: "+DemoString);
    }
}

The code above will convert the ByteArrayOutputStream with ASCII value to the String. See the output below.

The converted String is: Hello! This is delftstack.com..

Example Two:

Here, we can write the code following four steps. First, create an object of ByteArrayOutputStream. Second, initialize an empty variable as a String variable.

Third, use the write method to copy the String to the OutputStream. Finally, initialize a final String in the following way.

String Final_String = new String(Output_Stream.toByteArray());

Let’s learn it by writing the following code.

package delftstack;

import java.io.*;

class Example2 {
    public static void main(String[] args) throws IOException {

        // declare ByteArrayOutputStream
        ByteArrayOutputStream Output_Stream = new ByteArrayOutputStream();

        // Initiale string
        String DemoString = "Hello! This is delftstack.com..";

        // write the bytes to the output stream
        Output_Stream.write(DemoString.getBytes());

        // convert the stream to byte array and use typecasting 
        String Final_String = new String(Output_Stream.toByteArray());

        // print the strings
        System.out.println("The Demo String is: "+DemoString);
        System.out.println("The final String is: "+Final_String);
    }
}

The code above will convert the ByteArrayOutputStream to a String. See the example below.

The Demo String is: Hello! This is delftstack.com..
The final String is: Hello! This is delftstack.com..
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java String