Open In App

ByteArrayOutputStream reset() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The reset() method of ByteArrayOutputStream class in Java is used to reset the ByteArrayOutputStream and make the count field of this ByteArrayOutputStream to zero. As a result of this all currently accumulated output in this ByteArrayOutputStream is discarded. This ByteArrayOutputStream can be used again by reusing the already allocated buffer space.

Syntax:

public void reset()

Parameters: This method does not accept any parameter.

Return value: This method does not return any value.

Exceptions: This method does not throw any exception.

Below programs illustrate reset() method in ByteArrayOutputStream class in IO package:

Program 1:




// Java program to illustrate
// ByteArrayOutputStream reset() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create two byte arrays
        byte[] buf1 = { 65, 66, 67, 68, 69 };
        byte[] buf2 = { 71, 69, 69, 75, 83 };
  
        // Write first byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf1);
  
        // Print the byteArrayOutputStream
        // as String
        System.out.println(
            byteArrayOutStr.toString());
  
        // reset() is called
        byteArrayOutStr.reset();
  
        // Write second byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf2);
  
        // Print the byteArrayOutputStream
        // as String
        System.out.println(
            byteArrayOutStr.toString());
    }
}


Output:

ABCDE
GEEKS

Program 2:




// Java program to illustrate
// ByteArrayOutputStream reset() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create two byte arrays
        byte[] buf1 = { 71, 69, 69, 75, 83 };
        byte[] buf2 = { 70, 79, 82 };
  
        // Write first byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf1);
  
        // Print the byteArrayOutputStream
        // as String
        System.out.println(
            byteArrayOutStr.toString());
  
        // reset() is called
        byteArrayOutStr.reset();
  
        // Write second byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf2);
  
        // Print the byteArrayOutputStream
        // as String
        System.out.println(
            byteArrayOutStr.toString());
  
        // reset() is called
        byteArrayOutStr.reset();
  
        // Write first byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf1);
  
        // Print the byteArrayOutputStream
        // as String
        System.out.println(
            byteArrayOutStr.toString());
    }
}


Output:

GEEKS
FOR
GEEKS

References:
https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#reset()



Last Updated : 28 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads