ByteArrayOutputSteam close() method in Java with Examples Last Updated : 28 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The close() method of ByteArrayOutputStream class in Java has no effect if other methods of this class are called after closing the ByteArrayOutputStream. This method does not throw exception if even after closing the ByteArrayOutputStream, the other method is called. Syntax: public void close() Specified By: This method is specified by close() method of AutoCloseable interface and close() method of Closeable interface. Overrides:This method overrides close() method of OutputStream class. 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 close() method in ByteArrayOutputStream class in IO package: Program 1: Java // Java program to illustrate // ByteArrayOutputStream close() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { try { // Create byteArrayOutputStream ByteArrayOutputStream byteArrayOutStr = new ByteArrayOutputStream(); // Create byte array byte[] buf = { 71, 69, 69, 75, 83 }; // close() is called byteArrayOutStr.close(); // Write byte array // to byteArrayOutputStream byteArrayOutStr.write(buf); // Print the byteArrayOutputStream // as String System.out.println( byteArrayOutStr.toString()); } catch (Exception e) { System.out.println( "ByteArrayOutputStream is closed"); } } } Output: GEEKS Program 2: Java // Java program to illustrate // ByteArrayOutputStream close() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { try { // Create byteArrayOutputStream ByteArrayOutputStream byteArrayOutStr = new ByteArrayOutputStream(); // Create byte array byte[] buf1 = { 71, 69, 69, 75, 83 }; byte[] buf2 = { 70, 79, 82 }; // Write byte array // to byteArrayOutputStream byteArrayOutStr.write(buf1); // close() is called byteArrayOutStr.close(); // Write byte array // to byteArrayOutputStream byteArrayOutStr.write(buf2); // close() is called again byteArrayOutStr.close(); // Write byte array // to byteArrayOutputStream byteArrayOutStr.write(buf1); // Print the byteArrayOutputStream // as String System.out.println( byteArrayOutStr.toString()); } catch (Exception e) { System.out.println( "ByteArrayOutputStream is closed"); } } } Output: GEEKSFORGEEKS References: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#close() Comment More infoAdvertise with us Next Article BufferedInputStream close() method in Java with Examples P pp_pankaj Follow Improve Article Tags : Java Java-Functions Java-IO package Practice Tags : Java Similar Reads ByteArrayInputStream close() method in Java with Examples The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector. Syntax: public void close() Parameters: The function does not accepts any parameter. Return Value: The function returns 2 min read ByteArrayOutputStream reset() method in Java with Examples 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 2 min read ByteArrayOutputStream write() method in Java with Examples The write() method of ByteArrayOutputStream class in Java is used in two ways: 1. The write(int) method of ByteArrayOutputStream class in Java is used to write the specified byte to the ByteArrayOutputStream. This specified byte is passed as integer type parameter in this write() method. This write( 2 min read BufferedInputStream close() method in Java with Examples The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch 2 min read ByteArrayInputStream read() method in Java with Examples The read() method of ByteArrayInputStream class in Java is used in two ways: 1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read into the form of an integer and if the input stream is e 3 min read ByteArrayInputStream skip() method in Java with Examples The skip() method is a built-in method of the Java.io.ByteArrayInputStream which skips arg bytes in the input stream. A fewer bytes is skipped if the stream has reached towards the end. Syntax: public long skip(long args) Parameters: The function accepts a single mandatory parameter args which speci 2 min read Like