Skip to content
Related Articles

Related Articles

Improve Article
ByteArrayOutputStream reset() method in Java with Examples
  • Last Updated : 28 May, 2020

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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :