The perfect place for easy learning...

Java Programming

×

Topics List


Formatter class in java





The Formatter is a built-in class in java used for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output in java programming. The Formatter class is defined as final class inside the java.util package.

The Formatter class implements Cloneable and Flushable interface.

The Formatter class in java has the following constructors.

S.No. Constructor with Description
1 Formatter()

It creates a new formatter.

2 Formatter(Appendable a)

It creates a new formatter with the specified destination.

3 Formatter(Appendable a, Locale l)

It creates a new formatter with the specified destination and locale.

4 Formatter(File file)

It creates a new formatter with the specified file.

5 Formatter(File file, String charset)

It creates a new formatter with the specified file and charset.

6 Formatter(File file, String charset, Locale l)

It creates a new formatter with the specified file, charset, and locale.

7 Formatter(Locale l)

It creates a new formatter with the specified locale.

8 Formatter(OutputStream os)

It creates a new formatter with the specified output stream.

9 Formatter(OutputStream os, String charset)

It creates a new formatter with the specified output stream and charset.

10 Formatter(OutputStream os, String charset, Locale l)

It creates a new formatter with the specified output stream, charset, and locale.

11 Formatter(PrintStream ps)

It creates a new formatter with the specified print stream.

12 Formatter(String fileName)

It creates a new formatter with the specified file name.

13 Formatter(String fileName, String charset)

It creates a new formatter with the specified file name and charset.

14 Formatter(String fileName, String charset, Locale l)

It creates a new formatter with the specified file name, charset, and locale.

The Formatter class in java has the following methods.

S.No. Methods with Description
1 Formatter format(Locale l, String format, Object... args)

It writes a formatted string to the invoking object's destination using the specified locale, format string, and arguments.

2 Formatter format(String format, Object... args)

It writes a formatted string to the invoking object's destination using the specified format string and arguments.

3 void flush()

It flushes the invoking formatter.

4 Appendable out()

It returns the destination for the output.

5 Locale locale()

It returns the locale set by the construction of the invoking formatter.

6 String toString()

It converts the invoking object to string.

7 IOException ioException()

It returns the IOException last thrown by the invoking formatter's Appendable.

8 void close()

It closes the invoking formatter.

Let's consider an example program to illustrate methods of Formatter class.

Example
import java.util.*;

public class FormatterClassExample {

	public static void main(String[] args) {
		
		Formatter formatter=new Formatter();
		formatter.format("%2$5s %1$5s %3$5s", "Smart", "BTech", "Class");
		System.out.println(formatter);
		
		formatter = new Formatter();
		formatter.format(Locale.FRANCE,"%.5f", -1325.789);
		System.out.println(formatter);
		

	     String name = "Java";
	     formatter = new Formatter();
	     formatter.format(Locale.US,"Hello %s !", name);
	     System.out.println("" + formatter + " " + formatter.locale());
	     
	     formatter = new Formatter();
	     formatter.format("%.4f", 123.1234567); 
	     System.out.println("Decimal floating-point notation to 4 places: " + formatter); 
	     
	     formatter = new Formatter();
	     formatter.format("%010d", 88); 
	     System.out.println("value in 10 digits: " + formatter); 
	}
}

When we execute the above code, it produce the following output.

Formatter class in java