ByteArrayOutputStream toByteArray() in Java

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

Syntax:

byte[] java.io.ByteArrayOutputStream.toByteArray()

This method creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Parameters: NA

Returns: the current contents of this output stream, as a byte array.

Exceptions: NA

Approach

Java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

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

        int size = 5;
        ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(size);

        System.out.println(Arrays.toString(
byteArrayOutputStream.toByteArray()));
        byteArrayOutputStream.close();
    }
}

Output:

[]


No comments:

Post a Comment