Formatter close() in Java

close(): This method is available in java.util.Formatter class of Java.

Syntax:

void java.util.Formatter.close()

This method closes this formatter.

Parameters: NA

Note: Closing a formatter allows it to release resources it may be holding.

Parameters: NA

Exceptions: NA

Approach

Java

import java.util.Formatter;
import java.util.Locale;

public class Formatterclose {
    public static void main(String[] args) {

        StringBuffer buffer = new StringBuffer();

        Formatter formatter = new Formatter(buffer, Locale.UK);

        String name = "World";
        formatter.format("Hello %s", name);

        System.out.println(formatter + "");

        formatter.close();

        System.out.println(formatter + " ");
    }
}

Output:

Hello World Exception in thread "main" java.util.FormatterClosedException at java.base/java.util.Formatter.ensureOpen(Formatter.java:2561) at java.base/java.util.Formatter.toString(Formatter.java:2508) at java.base/java.lang.String.valueOf(String.java:3367) at java.base/java.lang.StringBuilder.append(StringBuilder.java:167)


No comments:

Post a Comment