The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Stream objects that implement formatting are instances of either 实现格式化的流对象是PrintWriter
, a character stream class, or PrintStream
, a byte stream class.PrintWriter
(字符流类)或PrintStream
(字节流类)的实例。
PrintStream
objects you are likely to need are System.out
and System.err
. PrintStream
对象是System.out
和System.err
。PrintWriter
, not PrintStream
. PrintWriter
,而不是PrintStream
。Like all byte and character stream objects, instances of 与所有字节和字符流对象一样,PrintStream
and PrintWriter
implement a standard set of write
methods for simple byte and character output. PrintStream
和PrintWriter
的实例为简单的字节和字符输出实现了一组标准的write
方法。In addition, both 此外,PrintStream
and PrintWriter
implement the same set of methods for converting internal data into formatted output. PrintStream
和PrintWriter
都实现了将内部数据转换为格式化输出的相同方法集。Two levels of formatting are provided:提供了两个级别的格式设置:
print
and println
format individual values in a standard way.print
和println
单个值。format
formats almost any number of values based on a format string, with many options for precise formatting.format
基于格式字符串格式化几乎任意数量的值,并提供许多用于精确格式化的选项。print
and println
Methodsprint
方法和println
方法Invoking 调用print
or println
outputs a single value after converting the value using the appropriate toString
method. print
或println
在使用适当的toString
方法转换值后输出单个值。We can see this in the 我们可以在Root
example:Root
示例中看到这一点:
public class Root { public static void main(String[] args) { int i = 2; double r = Math.sqrt(i); System.out.print("The square root of "); System.out.print(i); System.out.print(" is "); System.out.print(r); System.out.println("."); i = 5; r = Math.sqrt(i); System.out.println("The square root of " + i + " is " + r + "."); } }
Here is the output of 下面是Root
:Root
的输出:
The square root of 2 is 1.4142135623730951. The square root of 5 is 2.23606797749979.
The i
and r
variables are formatted twice: the first time using code in an overload of print
, the second time by conversion code automatically generated by the Java compiler, which also utilizes toString
. i
和r
变量被格式化两次:第一次使用print
的重载中的代码,第二次使用Java编译器自动生成的转换代码,Java编译器也使用toString
。You can format any value this way, but you don't have much control over the results.您可以用这种方式格式化任何值,但对结果没有太多控制权。
format
Methodformat
方法The format
method formats multiple arguments based on a format string. format
方法基于格式字符串格式化多个参数。The format string consists of static text embedded with format specifiers; except for the format specifiers, the format string is output unchanged.格式字符串由嵌入格式说明符的静态文本组成;除格式说明符外,格式字符串输出不变。
Format strings support many features. 格式字符串支持许多功能。In this tutorial, we'll just cover some basics. 在本教程中,我们将只介绍一些基础知识。For a complete description, see 有关完整说明,请参阅API规范中的格式字符串语法。format string syntax
in the API specification.
The Root2
example formats two values with a single format
invocation:Root2
示例使用单个format
调用格式化两个值:
public class Root2 { public static void main(String[] args) { int i = 2; double r = Math.sqrt(i); System.out.format("The square root of %d is %f.%n", i, r); } }
Here is the output:以下是输出:
The square root of 2 is 1.414214.
Like the three used in this example, all format specifiers begin with a 与本例中使用的三个一样,所有格式说明符都以%
and end with a 1- or 2-character conversion that specifies the kind of formatted output being generated. %
开头,以1或2个字符的转换结束,该转换指定生成的格式化输出的类型。The three conversions used here are:这里使用的三种转换是:
d
f
n
Here are some other conversions:以下是一些其他转换:
x
s
tB
There are many other conversions.还有许多其他的转换。
Except for 除%%
and %n
, all format specifiers must match an argument. %%
和%n
外,所有格式说明符必须与参数匹配。If they don't, an exception is thrown.如果没有,则抛出异常。
In the Java programming language, the 在Java编程语言中,\n
escape always generates the linefeed character (\u000A
). \n
转义符总是生成换行符(\u000A
)。Don't use 除非您特别想要换行符,否则不要使用\n
unless you specifically want a linefeed character. \n
。To get the correct line separator for the local platform, use 要获取本地平台的正确行分隔符,请使用%n
.%n
。
In addition to the conversion, a format specifier can contain several additional elements that further customize the formatted output. 除了转换之外,格式说明符还可以包含几个额外的元素,这些元素可以进一步自定义格式化输出。Here's an example, 这里有一个例子,Format
, that uses every possible kind of element.Format
,它使用了所有可能的元素。
public class Format { public static void main(String[] args) { System.out.format("%f, %1$+020.10f %n", Math.PI); } }
Here's the output:以下是输出:
3.141593, +00000003.1415926536
The additional elements are all optional. 附加元素都是可选的。The following figure shows how the longer specifier breaks down into elements.下图显示了较长说明符如何分解为元素。
Elements of a Format Specifier.格式说明符的元素。
The elements must appear in the order shown. Working from the right, the optional elements are:元素必须按显示的顺序显示。从右侧操作时,可选元素包括:
s
and other general conversions, this is the maximum width of the formatted value; the value is right-truncated if necessary.s
和其他常规转换,这是格式化值的最大宽度;如有必要,将右截断该值。Format
example, the +
flag specifies that the number should always be formatted with a sign, and the 0
flag specifies that 0
is the padding character. Format
示例中,+
标志指定数字应始终使用符号进行格式设置,0
标志指定0
是填充字符。-
(pad on the right) and ,
(format number with locale-specific thousands separators). -
(右边的pad)和,
(使用特定于区域设置的千位分隔符格式化数字)。<
to match the same argument as the previous specifier. <
与前面的说明符匹配相同的参数。System.out.format("%f, %<+020.10f %n", Math.PI);