Home » Java programming language

Java ByteArrayOutputStream close() Method with Example

ByteArrayOutputStream Class close() method: Here, we are going to learn about the close() method of ByteArrayOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 28, 2020

ByteArrayOutputStream Class close() method

  • close() method is available in java.io package.
  • close() method is used to close this stream and it does not create any error when we call any other methods of this class after closing the stream.
  • close() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error
  • close() method does not throw an exception at the time of closing the stream.

Syntax:

    public void close();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void close()  method of ByteArrayInputStream

import java.io.*;

public class CloseOfBAOS {
 public static void main(String[] args) throws Exception {
  byte[] b_arr = {
   100,
   101,
   102,
   103
  };
  ByteArrayOutputStream BAOS = null;

  try {
   // Instantiates ByteArrayOutputStream
   BAOS = new ByteArrayOutputStream();

   // By using close() method isto
   // close the ByteArrayOutputStream 
   BAOS.close();

   // By using write() method is to
   // write b_arr to the BAOS
   BAOS.write(b_arr);

   // By using toString() method is 
   // to represent the b_arr as a string
   System.out.print("BAOS.write(b_arr): " + BAOS.toString());

  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {

   if (BAOS != null)
    BAOS.close();
  }
 }
}

Output

BAOS.write(b_arr): defg


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.