English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java ByteArrayOutputStream and its methods with the help of examples.
The ByteArrayOutputStream class in the java.io package can be used to write output data arrays (in bytes).
It inherits the OutputStream abstract class.
Note: Maintain an internal byte array to store data in ByteArrayOutputStream.
To create a byte array output stream, we must first import the java.io.ByteArrayOutputStream package. After importing the package, we can create the output stream.
//Create a ByteArrayOutputStream with a default size ByteArrayOutputStream out = new ByteArrayOutputStream();
Here, we create an output stream that writes data to a default size of32It is stored in a byte array. However, we can change the default size of the array.
//Create a ByteArrayOutputStream with a specified size ByteArrayOutputStream out = new ByteArrayOutputStream(int size);
Here, size specifies the length of the array.
The ByteArrayOutputStream class provides different implementations of the methods existing in the OutputStream class.
write(int byte) - Writes the specified byte to the output stream
write(byte[] array) - Writes the bytes in the specified array to the output stream
write(byte[] arr, int start, int length)- Writes the number of bytes equal to length starting from position start to the output stream of the array
writeTo(ByteArrayOutputStream out1) -Writes all data from the current output stream to the specified output stream
import java.io.ByteArrayOutputStream; class Main { public static void main(String[] args) { String data = "This is a line of text inside the string."; try { //Create an output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] array = data.getBytes(); //Writes data to the output stream out.write(array); //Retrieve data from the output stream in string format String streamData = out.toString(); System.out.println("Output stream: ", + streamData); out.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Output stream: This is a line of text inside the string.
In the above example, we created a byte array output stream named output.
ByteArrayOutputStream output = new ByteArrayOutputStream();
To write data to the output stream, we used the write() method.
NoteThe getBytes() method used in the program converts a string to a byte array.
toByteArray() - Returns the array existing in the output stream
toString() - Returns all data of the output stream in string format
For example,
import java.io.ByteArrayOutputStream; class Main { public static void main(String[] args) { String data = "This is data."; try { //Creates an output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); //Writes data to the output stream out.write(data.getBytes()); //Returns a byte array byte[] byteData = out.toByteArray(); System.out.print("Using toByteArray() data: ", for(int i=0; i<byteData.length; i++) { System.out.print((char)byteData[i]); } //Returns a string String stringData = out.toString(); System.out.println("\nUsing toString() data: ", + stringData); out.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Using toByteArray() data: This is data. Using toString() data: This is data.
In the above example, we created a byte array to store the data returned by the toByteArray() method.
Then use a for loop to access each byte in the array. Here, use type conversion to convert each byte to the corresponding character.
To close the output stream, we can use the close() method.
However, the close() method is invalid in the ByteArrayOutputStream class. Even if the close() method is called, we can still use the methods of this class.
Method | Content Description |
---|---|
size() | Return the Size of the Array in the Output Stream |
flush() | Clear Output Stream |