Formatter

Java-Formatter

Formatter

Formatter class is used to creating formatted output. It provides format conversions that let you display numbers, strings, and time and date in virtually any format you like. It operates in a manner similar to the C/C++ printf( ) function, which means that if you are familiar with C/C++, then learning to use Formatter will be very easy.

Formatter Constructors

Before you can use Formatter to format output, you must create a Formatter object. In general, Formatter works by converting the binary form of data used by a program into formatted text. It stores the formatted text in a buffer, the contents of which can be obtained by your program whenever they are needed. It is possible to let Formatter supply this buffer automatically, or you can specify the buffer explicitly when a Formatter object is created. It is also possible to have Formatter output its buffer to a file. The Formatter class defines many constructors. Some of those:

Formatter

Here, file parameter specifies a reference to an open file that will receive output. The csn parameter specifies the character set. If no character set is specified, then the default character set is used. The os parameter specifies a reference to an output stream that will receive output. When using a file, output is also written to the file. The buf specifies a buffer for the formatted output. If buf is null, then Formatter automatically allocates a StringBuilder to hold the formatted output. The loc parameter specifies a locale. If no locale is specified, the default locale is used. The filename parameter specifies the name of a file that will receive the formatted output. Perhaps the most widely used constructor is the first, which has no parameters. It automatically uses the default locale and allocates a StringBuilder to hold the formatted output.

Formatter Methods

Formatter

Formatting Basics

After you have created a Formatter, you can use it to create a formatted string. To do so, use the format( ) method. The format( ) has two forms. The first form is:

Formatter

The String format consists of two types of items. The first type is composed of characters that are simply copied to the output buffer. The second type contains format specifiers that define the way the subsequent arguments are displayed. In its simplest form, a format specifier begins with a percent sign followed by the format conversion specifier. All format conversion specifiers consist of a single character. For example, the format specifier for integer data is %d. In general, there must be the same number of arguments as there are format specifiers, and the format specifiers and the arguments are matched in order from left to right. For example:

Formatter

In this example, the format specifiers, %s, and %d, are replaced with the arguments that follow the format string. Thus, %s is replaced by “Java”, and %d is replaced by 14. All other characters are simply used as-is. The format( ) method accepts a wide variety of format specifiers. It is important to understand that Java type-checks each format specifier against its corresponding argument. If the argument doesn’t match, an IllegalFormatException is thrown. For example:

Formatter

Once you have formatted a string, you can obtain it by calling toString( ). For example:

Formatter

If you simply want to display the formatted string, there is no reason to first assign it to a String object. When a Formatter object is passed to println( ), for example, its toString( ) method is automatically called. For example:

Formatter

You can obtain a reference to the underlying output buffer by calling out( ). It returns a reference to an Appendable object. For example:

Formatter

In the first line, the constructor has no parameters so automatically uses the default locale and allocates a StringBuilder to hold the formatted output.

In general, you should close a Formatter when you are done using it. Doing so frees any resources that it was using. This is especially important when formatting to a file, but it can be important in other cases, too. One way to close a Formatter is to explicitly call close( ).

General Format Specifiers

Formatter
Notice that many specifiers have both upper and lowercase forms. When an uppercase specifier is used, then letters are shown in uppercase. Otherwise, the upper and lowercase specifiers perform the same conversion.
Program

Formatter
Program Source

import java.util.Formatter;

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

        Formatter fmt = new Formatter();
        fmt.format("%s %d has New Features", "Java",14);
        System.out.println(fmt);
        fmt.close();
    }
}

Leave a Comment