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发行说明。
Objects of type 类型Scanner
are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.Scanner
的对象用于将格式化输入分解为令牌,并根据其数据类型转换单个令牌。
By default, a scanner uses white space to separate tokens. 默认情况下,扫描程序使用空白分隔令牌。(White space characters include blanks, tabs, and line terminators. (空白字符包括空格、制表符和行终止符。For the full list, refer to the documentation for 有关完整列表,请参阅Character.isWhitespace
.) Character.isWhitespace
的文档。)To see how scanning works, let's look at 为了了解扫描是如何工作的,让我们看看ScanXan
, a program that reads the individual words in xanadu.txt
and prints them out, one per line.ScanXan
,它是一个程序,读取xanadu.txt
中的单个单词并打印出来,每行一个。
import java.io.*; import java.util.Scanner; public class ScanXan { public static void main(String[] args) throws IOException { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); while (s.hasNext()) { System.out.println(s.next()); } } finally { if (s != null) { s.close(); } } } }
Notice that 请注意,ScanXan
invokes Scanner
's close
method when it is done with the scanner object. ScanXan
在处理Scanner
对象时调用Scanner
的close
方法。Even though a scanner is not a stream, you need to close it to indicate that you're done with its underlying stream.即使扫描器不是一个流,您也需要关闭它以表明您已经完成了它的底层流。
The output of ScanXan
looks like this:ScanXan
的输出如下所示:
In Xanadu did Kubla Khan A stately pleasure-dome ...
To use a different token separator, invoke 要使用不同的标记分隔符,请调用useDelimiter()
, specifying a regular expression. useDelimiter()
,指定正则表达式。For example, suppose you wanted the token separator to be a comma, optionally followed by white space. 例如,假设您希望标记分隔符是逗号,可以选择后跟空格。You would invoke,你会调用,
s.useDelimiter(",\\s*");
The ScanXan
example treats all input tokens as simple String
values. ScanXan
示例将所有输入标记视为简单的String
值。Scanner
also supports tokens for all of the Java language's primitive types (except for char
), as well as BigInteger
and BigDecimal
. Scanner
还支持所有Java语言原语类型(除char
外)的标记,以及BigInteger
和BigDecimal
。Also, numeric values can use thousands separators. 此外,数值可以使用数千个分隔符。Thus, in a 因此,在US
locale, Scanner
correctly reads the string "32,767" as representing an integer value.US
(美国)语言环境中,Scanner
将字符串“32,767”正确读取为表示整数值。
We have to mention the locale, because thousands separators and decimal symbols are locale specific. 我们必须提到区域设置,因为数千个分隔符和十进制符号是特定于区域设置的。So, the following example would not work correctly in all locales if we didn't specify that the scanner should use the 因此,如果我们没有指定扫描器应该使用US
locale. US
语言环境,那么下面的示例将无法在所有语言环境中正常工作。That's not something you usually have to worry about, because your input data usually comes from sources that use the same locale as you do. 这不是您通常需要担心的事情,因为您的输入数据通常来自与您使用相同语言环境的源。But this example is part of the Java Tutorial and gets distributed all over the world.但是这个例子是Java教程的一部分,并在全世界范围内发布。
The ScanSum
example reads a list of double
values and adds them up. ScanSum
示例读取一个double
值列表并将其相加。Here's the source:以下是消息来源:
import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.util.Scanner; import java.util.Locale; public class ScanSum { public static void main(String[] args) throws IOException { Scanner s = null; double sum = 0; try { s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt"))); s.useLocale(Locale.US); while (s.hasNext()) { if (s.hasNextDouble()) { sum += s.nextDouble(); } else { s.next(); } } } finally { s.close(); } System.out.println(sum); } }
And here's the sample input file, 这是示例输入文件,usnumbers.txt
8.5 32,767 3.14159 1,000,000.1
The output string is "1032778.74159". 输出字符串为“1032778.74159”。The period will be a different character in some locales, because 在某些区域设置中,句点将是不同的字符,因为System.out
is a PrintStream
object, and that class doesn't provide a way to override the default locale. System.out
是一个PrintStream
对象,并且该类不提供重写默认区域设置的方法。We could override the locale for the whole program or we could just use formatting, as described in the next topic, Formatting.我们可以覆盖整个程序的区域设置或者我们可以使用格式,如下一个主题格式化中所述。