Java ByteArrayOutputStream ClassLast Updated : 23 Jan 2026 The ByteArrayOutputStream class in Java is used to write data into a byte array, which can later be written to multiple output streams. In this chapter, we will learn what the ByteArrayOutputStream class is, how it works, its declaration, constructors, methods, and how to write the same data into multiple files using an example. What is ByteArrayOutputStream in Java?The ByteArrayOutputStream class writes data into an internal byte array instead of directly writing it to a file or device. The stored data can later be written to multiple output streams. This class maintains a copy of data in memory and forwards it to multiple streams when required. The internal buffer automatically grows as more data is written. ByteArrayOutputStream Class DeclarationThe following declaration shows how the ByteArrayOutputStream class is defined: Constructors of ByteArrayOutputStream ClassThe ByteArrayOutputStream class provides constructors to create a byte array output stream. 1. ByteArrayOutputStream()This constructor creates a new byte array output stream with an initial buffer capacity of 32 bytes. The buffer size increases automatically if required. Syntax: Here is the syntax of this constructor: 2. ByteArrayOutputStream(int size)This constructor creates a new byte array output stream with the specified initial buffer size. Syntax: Here is the syntax of this constructor: Example of ByteArrayOutputStream Class ConstructorsThe following code statements demonstrates how the ByteArrayOutputStream constructors are used to store data in memory. Methods of ByteArrayOutputStream ClassThe ByteArrayOutputStream class provides methods to write, convert, and transfer data.
Example of ByteArrayOutputStream ClassThis example demonstrates how to write the same data into two different files using the ByteArrayOutputStream class. Output: Success... f1.txt: A f2.txt: A ![]() Next TopicJava ByteArrayInputStream Class |
We request you to subscribe our newsletter for upcoming updates.