
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Formatter Class
Introduction
The Java Formatter class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output.Following are the important points about Formatter −
Formatters are not necessarily safe for multithreaded access.Thread safety is optional and is the responsibility of users of methods in this class.
Class declaration
Following is the declaration for java.util.Formatter class −
public final class Formatter extends Object implements Closeable, Flushable
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
Formatter() This constructor constructs a new formatter. |
2 |
Formatter(Appendable a) This constructor constructs a new formatter with the specified destination. |
3 |
Formatter(Appendable a, Locale l) This constructor constructs a new formatter with the specified destination and locale. |
4 |
Formatter(File file) This constructor constructs a new formatter with the specified file. |
5 |
Formatter(File file, String csn) This constructor constructs a new formatter with the specified file and charset. |
6 |
Formatter(File file, String csn, Locale l) This constructor constructs a new formatter with the specified file, charset, and locale. |
7 |
Formatter(File file, Charset charset, Locale l) This constructor constructs a new formatter with the specified file, charset, and locale.. |
8 |
Formatter(Locale l) This constructor constructs a new formatter with the specified locale. |
9 |
Formatter(OutputStream os) This constructor constructs a new formatter with the specified output stream. |
10 |
Formatter(OutputStream os, String csn) This constructor constructs a new formatter with the specified output stream and charset. |
11 |
Formatter(OutputStream os, String csn, Locale l) This constructor constructs a new formatter with the specified output stream, charset, and locale. |
12 |
Formatter(OutputStream os, Charset charset, Locale l) This constructor constructs a new formatter with the specified output stream, charset, and locale. |
13 |
Formatter(PrintStream ps) This constructor constructs a new formatter with the specified print stream. |
14 |
Formatter(String fileName) This constructor constructs a new formatter with the specified file name. |
15 |
Formatter(String fileName, String csn) This constructor constructs a new formatter with the specified file name and charset. |
16 | Formatter(String fileName, Charset charset, Locale l) This constructor constructs a new formatter with the specified file name, charset, and locale. |
17 |
Formatter(String fileName, String csn, Locale l) This constructor constructs a new formatter with the specified file name, charset, and locale. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
void close()
This method closes this formatter. |
2 |
void flush()
This method flushes this formatter. |
3 |
Formatter format(Locale l, String format, Object... args)
This method writes a formatted string to this object's destination using the specified locale, format string, and arguments. |
4 |
Formatter format(String format, Object... args)
This method writes a formatted string to this object's destination using the specified format string and arguments. |
5 |
IOException ioException()
This method returns the IOException last thrown by this formatter's Appendable. |
6 |
Locale locale()
This method returns the locale set by the construction of this formatter. |
7 |
Appendable out()
This method returns the destination for the output. |
8 |
String toString()
This method returns the result of invoking toString() on the destination for the output. |
Methods inherited
This class inherits methods from the following classes −
- java.util.Object
Formatting a String using US Locale Example
The following example shows the usage of Java Formatter format(String, Object) method to format a string using a formatter. We've created a formatter object with a StringBuffer and a locale. Formatter is used to print a string using the format() method.
package com.tutorialspoint; import java.util.Formatter; import java.util.Locale; public class FormatterDemo { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "World"; formatter.format("Hello %s !", name); // print the formatted string System.out.println(formatter); formatter.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
Hello World !