Java - ByteArrayOutputStream reset()



Description

Calling Java ByteArrayOutputStream reset() method resets the stream and makes the stream count = 0. Resetting this stream discards all currently accumulated output in it.

Declaration

Following is the declaration for java.io.ByteArrayOutputStream.reset() method −

public void reset()

Parameters

NA

Return Value

This method doesn't return any value.

Exception

NA

Example 1

The following example shows the usage of Java ByteArrayOutputStream reset() method. We've created a ByteArrayOutputStream reference and then initialized it with ByteArrayOutputStream object. Now we write a value to output stream and print the stream using toString() method. As a next step, we've reset the output stream to be empty and write a new value and printed it again.

package com.tutorialspoint;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;      
      try {
         String str = "";
         
         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();
         
         // writing byte to output stream
         baos.write(75);
         
         // output stream to string
         str = baos.toString();
         System.out.println("Before Resetting : "+str);
         
         // reset() method invocation 
         baos.reset();
         
         // writing byte to output stream
         baos.write(65);
         
         // output stream to string()
         str = baos.toString();
         System.out.println("After Resetting : "+ str);
      
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(baos!=null)
            baos.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Before Resetting : K
After Resetting : A

Example 2

The following example shows the usage of Java ByteArrayOutputStream reset() method. We've created a ByteArrayOutputStream reference and then initialized it with ByteArrayOutputStream object. Now we're writing multiple value to output stream and print the stream using toString() method. As a next step, we've reset the output stream to be empty and printed it again.

package com.tutorialspoint;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      
      try {
         String str = "";
         
         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();
         
         // writing bytes to output stream
         baos.write(75);
         baos.write(65);
         
         // output stream to string
         str = baos.toString();
         System.out.println("Before Resetting : "+str);
         
         // reset() method invocation 
         baos.reset();
         
         // output stream to string()
         str = baos.toString();
         System.out.println("After Resetting : "+ str);
      
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(baos!=null)
            baos.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Before Resetting : KA
After Resetting : 
java_bytearrayoutputstream.htm
Advertisements