当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

<b>JDK5新特点之新的格局化输出</b>[Java编程]

赞助商链接



  本文“<b>JDK5新特点之新的格局化输出</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
JDK5.0答应象C语言那样直接用printf()办法来格局化输出,并且供应了很多参数来格局化输入,调用也很简单:

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);

同时,从前的formatter类也供应了更完善的办法来格局化,比方:

formatter.format("Pi is approximately %1$f," +

"and e is about %2$f", Math.PI, Math.E);


格局化元素的构成以下:

%[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

以下是个例子:

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

对时间的格局化用字母t来代表,普通在t背面加上特别的字符来显示时间的某个部份:

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 " +

"%<tA, %<tB %<te, %<tY.%n",

Calendar.getInstance());

}

}

//阐明:“<”指导采取的参数为前一个被格局化的参数

//输出

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


  以上是“<b>JDK5新特点之新的格局化输出</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .