Signup/Sign In

Java ByteArrayOutputStream Class

In this tutorial, we will learn about ByteArrayOutputStream class in Java. This class is used to write the common data into multiple files. This class stream firstly converts the data into a byte array and that array can be converted into many streams when required.

enlightenedAn interesting fact to know about this class is the buffer of this class grows automatically according to the data.

Syntax

This is the class declaration of the ByteArrayOutputStream class which also inherits OutputStream.

public class ByteArrayOutputStream extends OutputStream  

Java ByteArrayOutputStream class Constructors

This class has two variants of constructors, the first one has an initial capacity of 32 bytes and it increases according to requirement, on the other hand for the second constructor we can implicitly pass the size of the buffer.

Constructor Description
ByteArrayOutputStream() Creates a new byte array output stream with the initial capacity of 32 bytes, though its size increases if necessary.
ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Java ByteArrayOutputStream class Methods

various methods of this class are given in the following table.

Method Description
int size() This method is used to returns the current size of a buffer.
byte[] toByteArray() This method is used to create a newly allocated byte array.
String toString() This method is used for converting the content into string decoding bytes using a platform default character set.
String toString(String charsetName) This method is used for converting the content into string decoding bytes using a specified charsetName.
void write(int b) This method is used for writing the byte specified to the byte array output stream.
void write(byte[] b, int off, int len) This method is used for writing len bytes from a specified byte array starting from the offset off to the byte array output stream.
void writeTo(OutputStream out) This method is used for writing the complete content of a byte array output stream to the specified output stream.
void reset() This method is used to reset the count field of a byte array output stream to zero value.
void close() This method is used to close the ByteArrayOutputStream.

Example of Java ByteArrayOutputStream

In the following example, we are going to write the same data into multiple files using writeTo() method of ByteArrayOutputStream class.

Firstly we created an object of a FileOutputStream class. Finally, using writeTo() method we are writing given content to both the files. From the output, we can see the same text is present in both files.

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		FileOutputStream fout1=new FileOutputStream("E:\\studytonight\\myfile1.txt");    
		FileOutputStream fout2=new FileOutputStream("E:\\studytonight\\myfile2.txt");    
		ByteArrayOutputStream bout=new ByteArrayOutputStream();    
		bout.write('A');    
		bout.writeTo(fout1);    
		bout.writeTo(fout2);    
		bout.flush();       
		System.out.println("Data written to the file...");    
	}    
}  


Data written to the file...

myfile1.txt

A

myfile2.txt

A

Conclusion

In this tutorial, we learned about ByteArrayOutputStream class in Java. By using this class we can write the common data into multiple files. This class supports various methods like size(), toByteArray(), toString(), toString(String charsetName), write(int b), write(byte[] b, int off, int len) etc.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.