Open In App

ByteArrayOutputStream write() method in Java with Examples

The write() method of ByteArrayOutputStream class in Java is used in two ways:

1. The write(int) method of ByteArrayOutputStream class in Java is used to write the specified byte to the ByteArrayOutputStream. This specified byte is passed as integer type parameter in this write() method. This write() method writes single byte at a time.



Syntax:

public void write(int b)

Specified By: This method is specified by write() method of OutputStream class.



Parameters: This method accepts one parameter b which represents the byte to be written.

Return value: The method does not return any value.

Exceptions: This method does not throw any exception.

Below program illustrates write(int) method in ByteArrayOutputStream class in IO package:

Program:




// Java program to illustrate
// ByteArrayOutputStream write(int) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Write byte
        // to byteArrayOutputStream
  
        byteArrayOutStr.write(71);
  
        byteArrayOutStr.write(69);
  
        byteArrayOutStr.write(69);
  
        byteArrayOutStr.write(75);
  
        byteArrayOutStr.write(83);
  
        // Print the byteArrayOutputStream
        System.out.println(
            byteArrayOutStr.toString());
    }
}

Output:
GEEKS

2. The write(byte[ ], int, int) method of ByteArrayOutputStream class in Java is used to write the given number of bytes from the given byte array starting at given offset of the byte array to the
ByteArrayOutputStream. This method is different from the above write() method as it can write several bytes at a time.

Syntax:

public void write(byte[ ] b,
                  int offset,
                  int length)

Overrides: This method overrides write() method of OutputStream class.

Parameters: This method accepts three parameters:

Return value: The method does not return any value.

Exceptions: This method does not throw any exception.

Below program illustrates write(byte[ ], int, int) method in ByteArrayOutputStream class in IO package:

Program:




// Java program to illustrate
// ByteArrayOutputStream
// write(byte[ ], int, int) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83,
                       70, 79, 82, 71, 69,
                       69, 75, 83 };
  
        // Write byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf, 8, 5);
  
        // Print the byteArrayOutputStream
        System.out.println(
            byteArrayOutStr.toString());
    }
}

Output:
GEEKS

References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#write(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#write(byte%5B%5D, int, int)


Article Tags :