ByteArrayOutputStream toString() in Java

toString(): This method is available in the java.io.ByteArrayOutputStream class of Java.

Syntax:

String java.io.ByteArrayOutputStream.toString()

This method converts the buffer's contents into string decoding bytes using the platform's default character set.

Note: The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer.

Parameters: NA

Returns: String decoded from the buffer's contents.

Exceptions: NA

Approach

Java

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

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

        ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();

        byte[] buffer = { 'H', 'E', 'L', 'L', 'O' };
        byteArrayOutputStream.write(buffer);
        System.out.println(byteArrayOutputStream.toString());
        byteArrayOutputStream.close();
    }
}

Output:

HELLO


No comments:

Post a Comment