Thursday, October 16, 2025
HomeLanguagesJavaFormatter flush() method in Java with Examples

Formatter flush() method in Java with Examples

The flush() method is a built-in method of the java.util.Formatter which flushes the formatter. Writing any buffered output in its destination to the underlying operating system is referred to as Flushing.

Syntax:

public void flush()

Parameters: The function accepts no parameter.

Return Value: The function returns nothing, it just flushes the formatter. Hence the return type is void.

Errors and Exceptions: The function throws a FormatterClosedException when this function is used after closing the formatter.

Below is the implementation of the above function:

Program 1:




// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // Get the string Buffer
        StringBuffer buffer = new StringBuffer();
  
        // Object creation
        Formatter frmt
            = new Formatter(buffer,
                            Locale.CANADA);
  
        // Format a new string
        String name = "My name is Gopal Dave";
        frmt.format("What is your name? \n%s !",
                    name);
  
        // Print the Formatted string
        System.out.println(frmt);
  
        // flushes the formatter
        frmt.flush();
        System.out.println("Flushed");
    }
}


Output:

What is your name? 
My name is Gopal Dave !
Flushed

Program 2:




// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
        try {
            // Get the string Buffer
            StringBuffer buffer
                = new StringBuffer();
  
            // Object creation
            Formatter frmt
                = new Formatter(buffer,
                                Locale.CANADA);
  
            // Format a new string
            String name = "My name is Gopal Dave";
            frmt.format("What is your name? \n%s !",
                        name);
  
            // Print the Formatted string
            System.out.println(frmt);
  
            // closes the formatter
            frmt.close();
  
            // close the frmt
            frmt.flush();
            System.out.println("Flushed");
        }
        catch (Exception e) {
            System.out.println("\nException is: "
                               + e);
        }
    }
}


Output:

What is your name? 
My name is Gopal Dave !

Exception is: java.util.FormatterClosedException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#close()

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS