首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → JDK5新特性之二----新的格式化输出

JDK5新特性之二----新的格式化输出

摘要: JDK5新特性之二----新的格式化输出

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="729" border="0"> <tr> <td width="723"> </td> </tr> </table>

JDK5.0允许象C语言那样直接用printf()方法来格式化输出,并且提供了许多参数来格式化输入,调用也很简单:

<table width="588" border=1 cellPadding=0 cellSpacing=0 bgColor=#e6e6e6 style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #e6e6e6; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt"> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=584>

System.out.format("Pi is approximately %f", Math.Pi);

System.out.printf("Pi is approximately %f", Math.Pi);

printf()和 format() 方法具有相同的功能. System.out 是 java.io.PrintStream的实例. PrintStream, java.io.PrintWriter, 和 java.lang.String 每个类都有四个新的格式化方法:

format( String format, Object... args);

printf( String format, Object... args);

format( Locale locale, String format, Object... args);

printf( Locale locale, String format, Object... args);

</td> </tr> </table>

同时,以前的formatter类也提供了更完善的方法来格式化,例如:

<table width="571" border=1 cellPadding=0 cellSpacing=0 bgColor=#e6e6e6 style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #e6e6e6; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt"> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=567>

formatter.format("Pi is approximately %1$f," + "and e is about %2$f", Math.PI, Math.E);

</td> </tr> </table>

格式化元素的构成如下:

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #e6e6e6; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 bgColor=#e6e6e6 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

%[argument_index$][flags][width][.precision]conversion

其中:

argument_index是一个正整数,说明了参数的位置,1为取第一个参数

width表示输出的最小字母个数

precision代表数字的小数位数

conversion代表被格式化的参数的类型:

f float,

t time

d decimal

o octal

x hexadecimal

s general

c a Unicode character

</td> </tr> </table>

以下是个例子:

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #e6e6e6; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 bgColor=#e6e6e6 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

package format;

import java.util.Formatter;

public class UsingFormatter {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingFormatter ");

System.exit(0);

}

String format = args[0];

StringBuilder stringBuilder = new StringBuilder();

Formatter formatter = new Formatter(stringBuilder);

formatter.format("Pi is approximately " + format +

", and e is about " + format, Math.PI, Math.E);

System.out.println(stringBuilder);

}

}

//控制台调用

java format/UsingFormatter %f

//输出

Pi is approximately 3.141593, and e is about 2.718282

//控制台调用

java format/UsingFormatter %.2f

//输出

Pi is approximately 3.14, and e is about 2.72

//控制台调用

java format/UsingFormatter %6.2f

//输出(有空格来填补长度)

Pi is approximately 3.14, and e is about 2.72

//控制台调用

java format/UsingFormatter %1$.2f

//输出

Pi is approximately 3.14, and e is about 3.14

//改变区域设置

package format;

import java.util.Formatter;

import java.util.Locale;

public class UsingFormatter {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingFormatter <format string>");

System.exit(0);

}

String format = args[0];

StringBuilder stringBuilder = new StringBuilder();

Formatter formatter = new Formatter(stringBuilder,

Locale.FRANCE);

formatter.format("Pi is approximately " + format +

", and e is about " + format, Math.PI, Math.E);

System.out.println(stringBuilder);

}

}

//控制台调用

java format/UsingFormatter %.2f

//输出

Pi is approximately 3,14, and e is about 2,72

//采用format,printf的可替代写法

package format;

public class UsingSystemOut {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingSystemOut <format string>");

System.exit(0);

}

String format = args[0];

System.out.format("Pi is approximately " + format +

", and e is approximately " + format, Math.PI, Math.E);

}

}

//控制台调用

java format/UsingSystemOut %.2f%n

//输出

Pi is approximately 3.14

, and e is about 2.72

</td> </tr> </table>

对时间的格式化用字母t来代表,通常在t后面加上特殊的字符来显示时间的某个部分:

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #e6e6e6; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 bgColor=#e6e6e6 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

tr hour and minute,

tA the day of the week

tB the name of the month

te the number of the day of the month

tY the year

//eg.

package format;

import java.util.Calendar;

public class FormattingDates {

public static void main(String[] args) {

System.out.printf("Right now it is %tr on " +

"%

Calendar.getInstance());

}

}

//说明:“<”指示采用的参数为前一个被格式化的参数

//输出

Right now it is 01:55:19 PM on Wednesday, September 22, 2004.

</td> </tr> </table>
function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: 辗转相除法求最大公约数
后一篇: 安装java2平台专业版5.0

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved