buf . buf null, Formatter StringBuilder . loc . , . filename , . charset . , , . outF , . outStrm , . , .
:
Formatter format(String fmtString, Object...args) , args ( ), , fmtString. ;
Formatter format(Locale loc, String fmtString,
Object...args) , args, ,
fmtString. , l. ;
IOException ioException() , , IOException, . null;
Locale locale() ;
Appendable out() - ;
void flush() , . Formatter, ;
void close() Formatter, , . Formatter .
FormatterClosedEception;
String toString() String, .
:
%a | |
%b | () |
%c | |
%d | |
%h | - |
%e | |
%f | |
%g | : % %f |
%o | |
%n | |
%s | |
%t | |
%x | |
%% | % |
: %A ( %a). .
|
|
/* # 9: format():
SimpleFormatString.java */
package chapt07;
import java.util.Formatter;
public class SimpleFormatString {
public static void main(String[] args){
Formatter f = new Formatter(); //
// %S, %c
f.format("This %s is about %n%S %c", "book","java",'6');
System. out. print(f);
}
}
:
This book is about
JAVA 6
/* # 10: %x, %o, %a, %g: FormatterDemoNumber.java */
package chapt07;
import java.util.Formatter;
public class FormatterDemoNumber {
public static void main(String[] args) {
Formatter f = new Formatter();
f.format("Hex: %x, Octal: %o", 100, 100);
System. out. println(f);
f = new Formatter();
f.format("%a", 100.001);
System. out. println(f);
f = new Formatter();
for (double i = 1000; i < 1.0e+10; i *= 100) {
f.format("%g ", i);
System. out. println(f);
}
}
}
:
Hex: 64, Octal: 144
X1.90010624dd2f2p6
1000.00
1000.00 100000
1000.00 100000 1.00000e+07
1000.00 100000 1.00000e+07 1.00000e+09
long, Long, Calendar, Date.
.
%tH | (00 23) |
%tI | (1 12) |
%tM | (00 59) |
%tS | (00 59) |
%tL | (000 999) |
%tY | |
%ty | (00 99) |
%tB | () |
%tb%th | () |
%tm | (1 12) |
%tA | () |
%ta | () |
%td | (1 31) |
%tR | "%tH:%tM" |
%tT | "%tH:%tM:%tS" |
%tr | "%tI:%tM:%tS %Tp" %Tp = (AM PM) |
%tD | "%tm/%td/%ty" |
%tF | "%tY%tm%td" |
%tc | "%ta %tb %td %tT %tZ %tY" |
/* # 11: :
FormatterDemoTimeAndDate.java */
package chapt07;
import java.util.*;
public class FormatterDemoTimeAndDate {
public static void main(String args[]) {
Formatter f = new Formatter();
Calendar cal = Calendar. getInstance ();
// 12-
f.format("%tr", cal);
System. out. println(f);
//
f = new Formatter();
f.format("%tc", cal);
System. out. println(f);
//
f = new Formatter();
f.format("%tl:%tM", cal, cal);
|
|
System. out. println(f);
//
f = new Formatter();
f.format("%tB %tb %tm", cal, cal, cal);
System. out. println(f);
}
}
:
PM
06 15:28:08 EET 2006
3:28
01
%f, %, %g %s . . , %10.4f 10
. .
. , %5.7s . , .
.
/* # 12: : FormatterDemoFlags.java */
package chapt07;
import java.util.*;
public class FormatterDemoFlags {
public static void main(String[] args) {
Formatter f = new Formatter();
//
f.format("|%10.2f|", 123.123);
System. out. println(f);
//
// '-'
f = new Formatter();
f.format("|%-10.2f|", 123.123);
System. out. println(f);
f = new Formatter();
f.format("% (d", -100);
// ' ' '('
System. out. println(f);
f = new Formatter();
f.format("%,.2f", 123456789.34);
// ','
System. out. println(f);
f = new Formatter();
f.format("%.4f", 1111.1111111);
//
System. out. println(f);
f = new Formatter();
f.format("%.16s", "Now I know class java.util.Formatter");
//
System. out. println(f);
}
}
:
| 123,12|
|123,12 |
(100)
123 456 789,34
1111,1111
Now I know class
Formatter , , . , , , . , , . . , , .
% : N$. N , .
/* #13: :
FormatterDemoArguments.java */
package chapt07;
import java.util.Formatter;
public class FormatterDemoArguments {
public static void main(String[] args) {
Formatter f = new Formatter();
Number n[] = { 4, 2.2, 3, 1.1 };
f.format("%4$.1f %2$.1f %3$d %1$d", n[0], n[1],
n[2], n[3]);
System. out. println(f);
}
}
:
1,1 2,2 3 4
, printf() :
System. out. printf("%4$.1f %2$.1f %3$d %1$d", n[0], n[1],
n[2], n[3]);
StringTokenizer , , . : , , , . , =,;:.
|
|
StringTokenizer :