Saturday 10 September 2022

Convert OutputStream to String in Java

‘java.io.OutputStream’  is the abstract superclass of all the classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink/destination.

 

Following examples explain how to convert different a ByteArrayOutputStream object to string in Java.

 

Example 1: Convert ByteArrayOutputStream to string.

 

Get the byte array from ByteArrayOutputStream object.

byte[] byteArr = byteArrayOutputStream.toByteArray();

Initialize the string using byte array.

String str2 = new String(byteArr);

Find the below working application.

 

OutputStreamToStringDemo1.java

package com.sample.app.streams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class OutputStreamToStringDemo1 {

	public static void main(String[] args) throws IOException {
		try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
			String str1 = "Hello World!!!!";
			byteArrayOutputStream.write(str1.getBytes());

			byte[] byteArr = byteArrayOutputStream.toByteArray();
			String str2 = new String(byteArr);

			System.out.println("str1 : " + str1);
			System.out.println("str2 : " + str2);
		}

	}

}

Output

str1 : Hello World!!!!
str2 : Hello World!!!!

Example 2: Using toString method of ByteArrayOutputStream.

ByteArrayOutputString#toString method converts the buffer's contents into a string decoding bytes using the platform's default character set.

String str2 = byteArrayOutputStream.toString();

OutputStreamToStringDemo2.java

package com.sample.app.streams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class OutputStreamToStringDemo2 {

	public static void main(String[] args) throws IOException {
		try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
			String str1 = "Hello World!!!!";
			byteArrayOutputStream.write(str1.getBytes());

			String str2 = byteArrayOutputStream.toString();

			System.out.println("str1 : " + str1);
			System.out.println("str2 : " + str2);
		}

	}

}

Output

str1 : Hello World!!!!
str2 : Hello World!!!!

You can even set the character set to decode the byte array to string using following toString methods.

public synchronized String toString(Charset charset)

public synchronized String toString(String charsetName)

Converts the buffer's contents into a string by decoding the bytes using the specified charset.

 

An invocation of this method of the form

String str = byteArrayOutputStream.toString("UTF-8");

 

behaves in exactly the same way as the below expression.

String str = byteArrayOutputStream.toString(StandardCharsets.UTF_8);

 


OutputStreamToStringDemo3.java

package com.sample.app.streams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class OutputStreamToStringDemo3 {

	public static void main(String[] args) throws IOException {
		try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
			String str1 = "Hello World!!!!";
			byteArrayOutputStream.write(str1.getBytes());

			String str2 = byteArrayOutputStream.toString("UTF-8");
			String str3 = byteArrayOutputStream.toString(StandardCharsets.UTF_8);

			System.out.println("str1 : " + str1);
			System.out.println("str2 : " + str2);
			System.out.println("str3 : " + str3);
		}

	}

}

Output

str1 : Hello World!!!!
str2 : Hello World!!!!
str3 : Hello World!!!!



You may like

Copy the content of file to other location

Write byte array to a file

How to download a binary file in Java?

How to process a huge file line by line in Java?

How to get the directory size in Java?

No comments:

Post a Comment