Formatter类:printf 风格的格式字符串的解释程序。此类提供了对布局对齐和排列的支持,以及对数值、字符串和日期/时间数据的常规格式和特定于语言环境的输出的支持。支持诸如 byte、BigDecimal 和 Calendar 等常见 Java 类型。
1. 主要讲下字符串的格式化,上个例子:
package string;
import java.util.Formatter;
public class Turtle {
private String name;
private Formatter format;
public Turtle(String name, Formatter format) {
this.name = name;
this.format = format;
}
public void move(int x, int y) {
//%s 格式化字符串 %d格式化整数
format.format("%s The Turtle is at (%d, %d),\n", name, x, y);
}
public static void main(String[] args) {
Turtle t1 = new Turtle("xiaoqiang", new Formatter(System.out));
t1.move(0, 0);
t1.move(0, 3);
t1.move(0, 6);
t1.move(3, 6);
}
}
//output
xiaoqiang The Turtle is at (0, 0).
xiaoqiang The Turtle is at (0, 3).
xiaoqiang The Turtle is at (0, 6).
xiaoqiang The Turtle is at (3, 6).
2 .排版格式的控制能力
package string;
import java.util.Formatter;
public class Receipt {
private double total = 0;
private Formatter f = new Formatter(System.out);
public void printTitle() {
//-表示左对齐
f.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
f.format("%-15s %5s %10s\n", "---", "---", "---");
}
public void print(String item, int qty, double price) {
f.format("%-15.15s %5d %10.2f\n", item, qty, price);
total += price;
}
public void printTotal() {
f.format("%-15s %5s %10.2f\n", "Tax", "", total*0.06);
f.format("%-15s %5s %10s\n", "", "", "---");
f.format("%-15s %5s %10.2f\n", "Total", "", total*1.06);
}
public static void main(String[] args) {
Receipt r = new Receipt();
r.printTitle();
r.print("big ice", 2, 9);
r.print("little ice", 1, 1.25);
r.print("old ice", 5, 5.5);
r.printTotal();
}
}
//output
Item Qty Price
--- --- ---
big ice 2 9.00
little ice 1 1.25
old ice 5 5.50
Tax 0.95
---
Total 16.70
3.类型转换
package string;
import java.math.BigInteger;
import java.util.Formatter;
public class Conversion {
public static void main(String[] args) {
Formatter f = new Formatter(System.out);
char u = 'a';
System.out.println("--------u = 'a'");
f.format("s: %s\n", u); //字符串
f.format("c: %c\n", u); //Unicode 字符
f.format("b: %b\n", u); //boolean值
f.format("h: %h\n", u); //16进制哈希值
int v = 21;
System.out.println("\n----------v = 21");
f.format("d: %d\n", v); //十进制整数
f.format("c: %c\n", v); //Unicode 字符
f.format("b: %b\n", v); //boolean值
f.format("s: %s\n", v); //字符串
f.format("x: %x\n", v); //16进制整数
f.format("h: %h\n", v); //16进制哈希值 hasCode-->toHex
BigInteger w = new BigInteger("5000000000000");
System.out.println("\n-------w = new BigInteger(\"5000000000000\")");
f.format("d: %d\n", w); //十进制整数
f.format("b: %b\n", w); //boolean值
f.format("s: %s\n", w); //字符串
f.format("x: %x\n", w); //16进制整数
f.format("h: %h\n", w); //16进制哈希值 hasCode-->toHex
double x = 1234.567;
System.out.println("\n-------x = 1234.567");
f.format("b: %b\n", x); //boolean
f.format("s: %s\n", x); //字符串
f.format("f: %f\n", x); //float
f.format("e: %e\n", x); //计算机科学记数法表示的十进制数
f.format("h: %h\n", x); //16进制哈希值 hasCode-->toHex
Conversion c = new Conversion();
System.out.println("\n-------c = new Conversion()");
f.format("b: %b\n", c); //boolean
f.format("s: %s\n", c); //字符串
f.format("h: %h\n", c); //16进制哈希值 hasCode-->toHex
boolean b = false;
System.out.println("\n-------b = false");
f.format("b: %b\n", b); //boolean
f.format("s: %s\n", b); //字符串
f.format("h: %h\n", b); //16进制哈希值 hasCode-->toHex
}
}
4. String.format :静态方法,实际使用的Formatter类,可简化编程
package string;
public class Hex {
//byte[]数组做16进制输出
public static String format(byte[] bytes) {
StringBuilder sb = new StringBuilder();
int n = 0;
for(byte b : bytes) {
if(n % 16 == 0) {
sb.append(String.format("%05X : ", n));
}
sb.append(String.format("%02X ", b));
n++;
if(n % 16 == 0) {
sb.append("\n");
}
}
sb.append("\n");
return sb.toString();
}
public static void main(String[] args) {
System.out.println(format("xiaoqiangqumaidabinggun".getBytes()));
}
}
5. System.out.printf() System.out.format() 同样使用Formatter进行格式化输出
6. 格式化输出的语法JDK的API中很详细了,这里主要描述一些应用场景和类的关系