java编程思想 第十三 章 String的格式化输出

1.格式化的输出
1.1System.out.format()
Java SE5引入的format方法可用于PrintStream或者PrintWriter对象。其中也包括System.out对象。format()方法模仿自C的printf(),两者是等价的,以下展示三种方法输出坐标点:

public class SimpleFormat {
public static void main(String[] args) {
int x = 5;
double y = 5.332542;
// The old way:
System.out.println("Row 1: [" + x + " " + y + "]");
// The new way:
System.out.format("Row 1: [%d %f]\n", x, y);
// or
System.out.printf("Row 1: [%d %f]\n", x, y);
}
} 

/* Output:
Row 1: [5 5.332542]
Row 1: [5 5.332542]
Row 1: [5 5.332542]
可以看到format()和printf()是等价的,它们只需要一个简单的格式化字符串,加上一串参数即可,每个参数对应一个格式修饰符。

1.2Formatter类
在Java中,所有新的格式化功能都有java.util.Formatter类处理。可以将Formatter看作一个翻译器,它将你的格式化字符串与数据翻译成需要的结果。当你创建一个Formatter对象的时候,需要向其构造器传递一些信息,告诉它最终的结果将想哪里输出:

public class Turtle {
private String name;
private Formatter f;
public Turtle(String name, Formatter f) {
this.name = name;
this.f = f;
}
public void move(int x, int y) {
f.format("%s The Turtle is at (%d,%d)\n", name, x, y);
}
public static void main(String[] args) {
PrintStream outAlias = System.out;
Turtle tommy = new Turtle("Tommy",
new Formatter(System.out));
Turtle terry = new Turtle("Terry",
new Formatter(outAlias));
tommy.move(0,0);
terry.move(4,8);
tommy.move(3,4);
terry.move(2,5);
tommy.move(3,3);
terry.move(3,3);
}
} 

/* Output:
Tommy The Turtle is at (0,0)
Terry The Turtle is at (4,8)
Tommy The Turtle is at (3,4)
Terry The Turtle is at (2,5)
Tommy The Turtle is at (3,3)

所有的tommy都将输出到System.out中,而所有的terry则输出到System.out的一个别名中。Formatter构造器经过重载可以接受多种输出目的地,最常用的还是PrintStream()\OutputStream和File。

1.3格式化说明符
为了在插入数据是控制空格和对齐,需要更加精细的格式修饰符。格式如下:
[argument_index$][flags][width][.precision]conversion
最常见的应用是控制一个域的最小尺寸,它可以通过指定width来实现。Formatter对象通过在必要时添加空格,来确保一个域至少达到某个程度。在默认的情况下,数据是右对齐,不过可以通过使用“-”标志来改变对齐方向。与width相对的是precision,它用来指明最大尺寸。width可以应用于各种类型的数据转换,并且其行为方式都一样。precision则不然,不是所有的类型数据都能使用precision,而且,应用于不同类型的数据转换时,precision的意义也不同。在将precision应用于String时,它将表示打印String时输出字符的最大数量。而在precision应用于浮点数时,它表示小数部分要显示出来的位数,如果小数位数过多则舍入,太少则在尾部补零。由于整数没有小数部分,所以precision无法应用于整数。
下面是例子:

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");  //Item项都有“-”,表示左对齐,其余项都是右对齐
f.format("%-15s %5s %10s\n", "----", "---", "-----");
}
public void print(String name, int qty, double price) {
f.format("%-15.15s %5d %10.2f\n", name, qty, price);    //name是左对齐并且最多15个字符,包括空格。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 receipt = new Receipt();
receipt.printTitle();
receipt.print("Jack’s Magic Beans", 4, 4.25);
receipt.print("Princess Peas", 3, 5.1);
receipt.print("Three Bears Porridge", 1, 14.29);
receipt.printTotal();
}
} 

/* Output:
Item Qty Price


Jack’s Magic Be 4 4.25
Princess Peas 3 5.10
Three Bears Por 1 14.29

Tax 1.42

Total 25.06
由以上例子可以看出,Formatter提供了对空格与对齐的强大控制能力。为了恰当地控制间隔,格式化字符串被重复利用了多遍。
1.4 Formatter转换
常用的类型转换:
d 整型 (十进制)
c Unicode字符
b Boolean
s String
f 浮点型 (十进制)
e 浮点型(科学计数)
x 整数 (十六进制)
h 散列码(十六进制)
% 字符”%”

实例:

Formatter f = new Formatter(System.out);
char u = ‘a’;
f.format("c: %c\n", u);
f.format("b: %b\n", u);

所有类型变量都可以执行b转换,除了boolean基本类型或Boolean对象对应相应的true/false,其它的只要参数不为null,结果永远是true。(参数为数字0,仍然是true,这点与C不同)

1.5 String.format()
String.format()是一个static方法,它接受与Formatter.format()方法一样的参数,但返回一个String对象。
如下所示:
String.format(“(t%d, q%d) %s”, transactionID,queryID, message)
其实在String.format()内部,它也是一个创建Formatter对象,然后将你传入的参数转给Formatter。不过,使用便捷的String.format()方法,更清晰易读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值