The toByteArray() method of ByteArrayOutputStream class in Java is used to create a newly allocated byte array. The size of the newly allocated byte array is equal to the current size of this output stream. This method copies valid contents of the buffer into it.
public byte toByteArray()
Parameters: This method does not accept any parameter.
Return value: The method returns newly allocated byte array which has the valid contents of this output stream.
Exceptions: This method does not throw any exception.
Below programs illustrate toByteArray() method in ByteArrayOutputStream class in IO package:
G E E K S
G E E K S F O R G E E K S
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.