BufferedOutputStream flush() method in Java with Examples

The flush() method of BufferedOutputStream class in Java is used to flush the buffered output stream. This method is used to compel the bytes of buffered output to be written out to the main output stream.

Syntax:

public void flush() 
            throws IOException

Specified By: This method is specified by the flush() method in Flushable interface.

Overrides: This method overrides the flush() method in FilterOutputStream class.

Parameters: This method does not accept any parameter.



Return value: This method does not return any value.

Exception: This method throws IOException if an I/O error occurs.

Below programs illustrate flush() method in BufferedOutputStream class in IO package:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// BufferedOutputStream flush() method
import java.io.*;
public class GFG {
    public static void main(String[] args) throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream
            byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Convert outputStream to
        // bufferedInputStream
        BufferedOutputStream buffOutputStr
            = new BufferedOutputStream(
                byteArrayOutStr);
  
        // Creating byte array
        byte b[] = { 71, 69, 69, 75, 83 };
        buffOutputStr.write(b);
  
        // flush is called
        // to compel bytes to be
        // written out to buffOutputStr
        buffOutputStr.flush();
  
        for (byte by : byteArrayOutStr
                           .toByteArray()) {
  
            // Converts byte to character
            char ch = (char)by;
            System.out.print(ch);
        }
    }
}

chevron_right


Output:

GEEKS

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// BufferedOutputStream flush() method
import java.io.*;
public class GFG {
    public static void main(String[] args) throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr = new ByteArrayOutputStream();
  
        // Convert outputStream to
        // bufferedInputStream
        BufferedOutputStream buffOutputStr
            = new BufferedOutputStream(
                byteArrayOutStr);
  
        // Creating first byte array
        byte b1[] = { 71, 69, 69, 75, 83 };
        buffOutputStr.write(b1);
  
        // flush is called
        // to compel bytes to be
        // written out to buffOutputStr
        buffOutputStr.flush();
  
        // Creating second byte array
        byte b2[] = { 70, 79, 82 };
        buffOutputStr.write(b2);
  
        buffOutputStr.flush();
  
        buffOutputStr.write(b1);
  
        buffOutputStr.flush();
  
        for (byte by : byteArrayOutStr.toByteArray()) {
            // Converts byte to character
            char ch = (char)by;
            System.out.print(ch);
        }
    }
}

chevron_right


Output:

GEEKSFORGEEKS

References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedOutputStream.html#flush()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.