Java.io.ByteArrayOutputStream.reset() Method


Advertisements


Description

Calling java.io.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

The following example shows the usage of java.io.ByteArrayOutputStream.reset() method.

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

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

Before Resetting : K
After Resetting : A

java_io_bytearrayoutputstream.htm

Advertisements