At the core of Java’s support for creating formatted output is the Formatter class. It provides format conversions that let you display numbers, strings, and time and date in virtually any format
The Formatter Constructors
The Formatter class defines many constructors, which enable you to construct a Formatter in a variety of ways. Here is a sampling:
Formatter( )
Formatter(Appendable buf)
Formatter(Appendable buf, Locale loc)
Formatter(String filename) throws FileNotFoundException
Formatter(String filename, String charset)
throws FileNotFoundException, UnsupportedEncodingException
Formatter(File outF)
throws FileNotFoundException
Formatter(OutputStream outStrm)
The Formatter Methods
Method | Description | ||
void close( ) | Closes the invoking Formatter. This causes any resources | ||
used by the object to be released. After a Formatter has | |||
been closed, it cannot be reused. An attempt to use a | |||
closed Formatter results in a FormatterClosedException. | |||
void flush( ) | Flushes the format buffer. This causes any output currently | ||
in the buffer to be written to the destination. This applies | |||
mostly to a Formatter tied to a file. | |||
Formatter format(String fmtString, | Formats the arguments passed via args according to the format | ||
Object … args) | specifiers contained in fmtString. Returns the invoking object. | ||
Formatter format(Locale loc, | Formats the arguments passed via args according to the format | ||
String fmtString, | specifiers contained in fmtString. The locale specified by loc is | ||
Object … args) | used for this format. Returns the invoking object. | ||
IOException ioException( ) | If the underlying object that is the destination for output throws | ||
an IOException, then this exception is returned. Otherwise, | |||
null is returned. | |||
Locale locale( ) | Returns the invoking object’s locale. | ||
Appendable out( ) | Returns a reference to the underlying object that is the | ||
destination for output. | |||
String toString( ) | Returns a String containing the formatted output. | ||
Format Specifier | Conversion Applied |
%a | Floating-point hexadecimal |
%A | |
%b | Boolean |
%B | |
%c | Character |
%d | Decimal integer |
%h | Hash code of the argument |
%H | |
%e | Scientific notation |
%E | |
%f | Decimal floating-point |
%g | Uses %e or %f, whichever is shorter |
%G | |
%o | Octal integer |
%n | Inserts a newline character |
%s | String |
%S | |
%t | Time and date |
%T | |
%x | Integer hexadecimal |
%X | |
%% | Inserts a % sign |
A very simple example that uses Formatter
import java.util.*;
class FormatDemo {
public static void main(String args[]) { Formatter fmt = new Formatter();
fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6);
System.out.println(fmt);
}
}
Demonstrate the %g format specifier
import java.util.*;
class FormatDemo2 {
public static void main(String args[]) { Formatter fmt = new Formatter();
for(double i=1000; i < 1.0e+10; i *= 100) { fmt.format("%g ", i); System.out.println(fmt);
}
}
}
Output
1000.000000
1000.000000 100000.000000
1000.000000 100000.000000 1.000000e+07
1000.000000 100000.000000 1.000000e+07 1.000000e+09