ByteArrayOutputStream class in java

java.io.ByteArrayOutputStream is a class in java programming language. Inside of this class exists 10 public methods. Declaration of this class as follows.

public class java.io.ByteArrayOutputStream 
extends java.io.OutputStream 

ByteArrayOutputStream public method

There are following useful methods which is define the inside of java.io.ByteArrayOutputStream class.

MethodDescription
void close() It closing a ByteArrayOutputStream has no effect.
void reset() It resets the field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
int size() It returns the current size of the buffer.
byte[] toByteArray() It creates a newly allocated byte array.
String toString() It converts the buffer's contents into a string decoding bytes using the platform's default character set.
String toString(int hibyte) It deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the method, which takes an encoding-name argument, or the toString() method, which uses the platform's default character encoding.
String toString(String charsetName) It converts the buffer's contents into a string by decoding the bytes using the named charset.
void write(byte[] b, int off, int len) It writes bytes from the specified byte array starting at offset off to this byte array output stream.
void write(int b) It writes the specified byte to this byte array output stream.
void writeTo(OutputStream out) It writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

This reference is belong to javadoc

Public Constructors

There is 2 types of public constructor is defined inside the class, Whose syntax as follows.

public java.io.ByteArrayOutputStream()
public java.io.ByteArrayOutputStream(int)

Example of java.io.ByteArrayOutputStream() constructor.

// Java program for ByteArrayOutputStream
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
public class Example
{
    public static void main(String[] args) throws Exception
    {
        byte[] data = {
            65 , 66 , 67
        };
        FileOutputStream file = new FileOutputStream("file.txt");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(data, 0, data.length);
        // Write file value
        os.writeTo(file);
        os.close();
        file.close();
    }
}
// file.text 
// Add values
// ------ 
// ABC
Instance of java.io.ByteArrayOutputStream class in java


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved