formatter.html

来自「j2se5-api-zh,java文档的中文版本」· HTML 代码 · 共 1,086 行 · 第 1/5 页

HTML
1,086
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc (build 1.5.0_03) on Wed Nov 02 09:50:48 CST 2005 --><META http-equiv="Content-Type" content="text/html; charset=gb2312"><TITLE>Formatter (Java 2 Platform SE 5.0)</TITLE><META NAME="keywords" CONTENT="java.util.Formatter class"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){    parent.document.title="Formatter (Java 2 Platform SE 5.0)";}</SCRIPT><NOSCRIPT></NOSCRIPT></HEAD><BODY BGCOLOR="white" onload="windowTitle();"><!-- ========= START OF TOP NAVBAR ======= --><A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="跳过导航链接"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY=""><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>概述</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>软件包</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>类</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/Formatter.html"><FONT CLASS="NavBarFont1"><B>使用</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>树</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>已过时</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>索引</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>帮助</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup>&nbsp;2&nbsp;Platform<br>Standard&nbsp;Ed. 5.0</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="FormattableFlags.html" title="java.util 中的类"><B>上一个类</B></A>&nbsp;&nbsp;<A HREF="Formatter.BigDecimalLayoutForm.html" title="java.util 中的枚举"><B>下一个类</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../index.html@java_2Futil_2FFormatter.html" target="_top"><B>框架</B></A>  &nbsp;&nbsp;<A HREF="Formatter.html" target="_top"><B>无框架</B></A>  &nbsp;&nbsp;<SCRIPT type="text/javascript">  <!--  if(window==top) {    document.writeln('<A HREF="../../allclasses-noframe.html"><B>所有类</B></A>');  }  //--></SCRIPT><NOSCRIPT>  <A HREF="../../allclasses-noframe.html"><B>所有类</B></A></NOSCRIPT></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  摘要:&nbsp;<A HREF="#nested_class_summary">嵌套</A>&nbsp;|&nbsp;字段&nbsp;|&nbsp;<A HREF="#constructor_summary">构造方法</A>&nbsp;|&nbsp;<A HREF="#method_summary">方法</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">详细信息:&nbsp;字段&nbsp;|&nbsp;<A HREF="#constructor_detail">构造方法</A>&nbsp;|&nbsp;<A HREF="#method_detail">方法</A></FONT></TD></TR></TABLE><A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.util</FONT><BR>类 Formatter</H2><PRE><A HREF="../lang/Object.html" title="java.lang 中的类">java.lang.Object</A>  <IMG SRC="../../resources/inherit.gif" ALT="继承者 "><B>java.util.Formatter</B></PRE><DL><DT><B>所有已实现的接口:</B> <DD>java.io.Closeable, java.io.Flushable</DD></DL><HR><DL><DT><PRE>public final class <B>Formatter</B><DT>extends <A HREF="../lang/Object.html" title="java.lang 中的类">Object</A><DT>implements java.io.Closeable, java.io.Flushable</DL></PRE><P>printf 风格的格式字符串的解释程序。此类提供了对布局对齐和排列的支持,以及对数值、字符串和日期/时间数据的常规格式和特定于语言环境的输出的支持。支持诸如 <tt>byte</tt>、<CODE>BigDecimal</CODE> 和 <A HREF="Calendar.html" title="java.util 中的类"><CODE>Calendar</CODE></A> 等常见 Java 类型。任意用户类型的受限格式化定制都是通过 <A HREF="Formattable.html" title="java.util 中的接口"><CODE>Formattable</CODE></A> 接口提供的。<p> Formatter 对于多线程访问而言没必要是安全的。线程安全是可选的,它对此类中的方法用户负责。<p> Java 语言的格式化输出在很大程度上受到 C 语言 <tt>printf</tt> 的启发。虽然一些格式字符串与 C 类似,但已进行了某些定制,以适应 Java 语言,并且利用了其中一些特性。此外,Java 的格式比 C 的格式更严格;例如,如果转换与标志不兼容,则会抛出异常。在 C 中,不适用的标志会被忽略。这样,便于 C 程序员识别这些格式字符串,而又不必与 C 中的那些标志完全兼容。<p> 所期望用法的示例: <blockquote><pre>   StringBuilder sb = new StringBuilder();   // Send all output to the Appendable object sb   Formatter formatter = new Formatter(sb, Locale.US);   // Explicit argument indices may be used to re-order output.   formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")   // -> " d  c  b  a"   // Optional locale as the first argument can be used to get   // locale-specific formatting of numbers.  The precision and width can be   // given to round and align the value.   formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);   // -> "e =    +2,7183"   // The '(' numeric flag may be used to format negative numbers with   // parentheses rather than a minus sign.  Group separators are   // automatically inserted.   formatter.format("Amount gained or lost since last statement: $ %(,.2f",                    balanceDelta);   // -> "Amount gained or lost since last statement: $ (6,217.58)" </pre></blockquote><p> 常见格式化请求的便捷方法是按照如下调用格式来阐明的: <blockquote><pre>   // Writes a formatted string to System.out.   System.out.format("Local time: %tT", Calendar.getInstance());   // -> "Local time: 13:34:18"   // Writes formatted output to System.err.   System.err.printf("Unable to open file '%1$s': %2$s",                     fileName, exception.getMessage());   // -> "Unable to open file 'food': No such file or directory" </pre></blockquote><p> 与 C 语言的 <tt>sprintf(3)</tt> 类似,可以使用静态方法 <CODE>String#format(String,Object...)String.format</CODE> 来格式化 Strings: <blockquote><pre>   // Format a string containing a date.   import java.util.Calendar;   import java.util.GregorianCalendar;   import static java.util.Calendar.*;   Calendar c = new GregorianCalendar(1995, MAY, 23);   String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);   // -> s == "Duke's Birthday: May 23, 1995" </pre></blockquote><a name="org"><h3> 组织 </h3></a><p> 此规范分为两部分。第一部分是<a href="#summary">摘要</a>,包括基本的格式概念。这一部分是为那些想要快速入门并熟悉其他编程语言的格式化输出的用户准备的。第二部分是<a href="#detail">详细信息</a>,包括具体的实现细节。它是为那些需要更精确格式化行为规范的用户准备的。<a name="summary"><h3> 摘要 </h3></a><p>这一部分将提供对格式概念的简单概述。有关精确的行为细节,请参阅<a href="#detail">详细信息</a>部分。<a name="syntax"><h4> 格式字符串的语法 </h4></a><p> 产生格式化输出的每个方法都需要<i>格式字符串</i> 和<i>参数列表</i>。格式字符串是一个 <A HREF="../lang/String.html" title="java.lang 中的类"><CODE>String</CODE></A>,它可以包含固定文本以及一个或多个嵌入的<i>格式说明符</i>。请考虑以下示例: <blockquote><pre>   Calendar c = ...;   String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c); </pre></blockquote>此格式字符串是 <tt>format</tt> 方法的第一个参数。它包含三个格式说明符 &quot;<tt>%1$tm</tt>&quot;、&quot;<tt>%1$te</tt>&quot; 和 &quot;<tt>%1$tY</tt>&quot;,它们指出应该如何处理参数以及在文本的什么地方插入它们。格式字符串的其余部分是包括 <tt>&quot;Dukes Birthday: &quot;</tt> 和其他任何空格或标点符号的固定文本。参数列表由传递给位于格式字符串之后的方法的所有参数组成。在上述示例中,参数列表的大小为 1,由新对象 <A HREF="Calendar.html" title="java.util 中的类"><CODE>Calendar</CODE></A> 组成。 <ul><li> 常规类型、字符类型和数值类型的格式说明符的语法如下: <blockquote><pre>   %[argument_index$][flags][width][.precision]conversion </pre></blockquote><p> 可选的 <i>argument_index</i> 是一个十进制整数,用于表明参数在参数列表中的位置。第一个参数由 &quot;<tt>1$</tt>&quot; 引用,第二个参数由 &quot;<tt>2$</tt>&quot; 引用,依此类推。<p> 可选的 <i>flags</i> 是修改输出格式的字符集。有效标志的集合取决于转换类型。<p> 可选 <i>width</i> 是一个非负十进制整数,表明要向输出中写入的最少字符数。<p> 可选 <i>precision</i> 是一个非负十进制整数,通常用来限制字符数。特定行为取决于转换类型。<p> 所需的 <i>conversion</i> 是一个表明应该如何格式化参数的字符。给定参数的有效转换集合取决于参数的数据类型。<li> 用来表示日期和时间类型的格式说明符的语法如下: <blockquote><pre>   %[argument_index$][flags][width]conversion </pre></blockquote><p> 可选的 <i>argument_index</i>、<i>flags</i> 和 <i>width</i> 的定义同上。<p> 所需的 <i>conversion</i> 是一个由两字符组成的序列。第一个字符是 <tt>'t'</tt> 或 <tt>'T'</tt>。第二个字符表明所使用的格式。这些字符类似于但不完全等同于那些由 GNU <tt>date</tt> 和 POSIX <tt>strftime(3c)</tt> 定义的字符。<li> 与参数不对应的格式说明符的语法如下: <blockquote><pre>   %[flags][width]conversion </pre></blockquote><p> 可选 <i>flags</i> 和 <i>width</i> 的定义同上。

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?