Java Scanner类

Java.util包提供了一个Scanner类。 简单的文本扫描器使用正则表达式解析原始类型和字符串。 扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格。 然后可以使用各种 next 方法将所得令牌转换为不同类型的值。
扫描操作可能会阻塞等待输入,并且在没有外部同步的情况下对于多线程使用不安全。

类声明

java.util.Scanner 类的声明是:

public final class Scanner
  extends Object
    implements Iterator<String>, Closeable

类构造函数

构造一个新的 Scanner,它生成从指定文件扫描的值。
序号构造函数和说明
1.Scanner(File source)
构造一个新的扫描程序,用于生成从指定文件扫描的值
2.Scanner(File source, String charsetName)
构造一个新的 Scanner生成从指定文件扫描的值.
3.Scanner(InputStream source)
构造一个新的生成从指定输入流扫描的值的扫描器.
4.

Scanner(InputStream source, String charsetName)

构造一个新的 Scanner,它生成从指定输入流扫描的值.

5.Scanner(Path source)
5.Scanner(Path source)
6.Scanner(Path source, String charsetName)
构造一个新的 Scanner,它生成从指定文件扫描的值.
7. SScanner(Readable source)
构造一个新的 Scanner,生成从指定源扫描的值.
8.Scanner(ReadableByteChannel source)
构造一个新的 Scanner,生成从指定通道扫描的值.
9.Scanner(ReadableByteChannel source, String charsetName)
构造一个新的 Scanner,生成从指定通道扫描的值.
10.Scanner(String source)
构造一个新的 Scanner,生成从指定字符串扫描的值.

java.util.Scanner 方法

java.util.Scanner 类有许多方法,如下所列:

成员方法

序号方法与方法说明
1.void close()
关闭Scanner.
2.Pattern delimiter()
返回模式扫描程序当前用于匹配分隔符.
3.String findInLine(Pattern pattern)
尝试查找指定模式的下一个匹配项,忽略分隔符.
4.String findInLine(String pattern)
尝试查找从指定字符串构造的模式的下一个匹配项,忽略分隔符.
5.String findWithinHorizon(Pattern pattern, int Horizon)
尝试查找指定模式的下一个匹配项。
6.String findWithinHorizon(String pattern, int Horizon)
尝试查找从指定字符串构造的模式的下一个匹配项,忽略分隔符.
7.boolean hasNext()
如果扫描器的输入中有另一个标记,则返回 true.
8.boolean hasNext(Pattern pattern)
如果下一个完整标记与指定模式匹配,则返回 true.
9 .boolean hasNext(String pattern)
如果下一个标记与从指定字符串构造的模式匹配,则返回 true。 td>
10.boolean hasNextBigDecimal()
如果下一个标记返回 trueScanner输入中的 可以使用 nextBigDecimal() 方法解释为 BigDecimal 值.
11.boolean hasNextBigInteger()
如果Scanner输入中的下一个标记可以使用 nextBigInteger() 方法解释为默认基数中的 BigInteger 值,则返回 true.
12.boolean hasNextBigInteger(int radix)
如果Scanner输入中存在下一个标记,则返回 true可以使用 nextBigInteger() 方法将其解释为指定基数中的 BigInteger 值.
13.boolean hasNextBoolean()
如果Scanner输入中的下一个标记可以使用从字符串“true|false”创建的不区分大小写的模式解释为布尔值,则返回 true.
14.boolean hasNextByte()
如果下一个标记在Scanner的输入可以使用 nextByte() 方法解释为默认基数中的字节值.
15.boolean hasNextByte(int radix)
如果Scanner输入中的下一个标记可以使用 nextByte() 方法解释为指定基数中的字节值,则返回 true.
16.boolean hasNextDouble()
如果下一个标记在Scanner的输入可以使用 nextDouble() 方法解释为双精度值.
17. boolean hasNextFloat()
如果Scanner输入中的下一个标记可以使用 nextFloat() 方法解释为浮点值,则返回 true.
18.boolean hasNextInt()
如果Scanner输入中的下一个标记可以解释为 int 值,则返回 true使用 nextInt() 方法的默认基数.
19.boolean hasNextInt(int radix)
如果使用 nextInt() 方法可以将扫描器输入中的下一个标记解释为指定基数中的 int 值,则返回 true.
20.boolean hasNextLine()
如果Scanner的输入中有另一行,则返回 true.
21.boolean hasNextLong()
如果下一个标记在使用 nextLong() 方法可以将Scanner的输入解释为默认基数中的长值.
22.boolean hasNextLong(int radix)
如果Scanner输入中的下一个标记可以使用 nextLong() 方法解释为指定基数中的 long 值,则返回 true.
23.boolean hasNextShort()
如果下一个标记在Scanner的输入可以使用 nextShort() 方法解释为默认基数中的短值.
24.boolean hasNextShort(int radix)
如果Scanner输入中的下一个标记可以使用 nextShort() 方法解释为指定基数中的短值,则返回 true.
25.IOException ioException()
返回Scanner最后抛出的 IOException底层可读.
26.Locale locale()
返回Scanner的区域设置.
27.MatchResult match()
返回Scanner上次扫描操作的匹配结果.
28.String next ()
从Scanner中查找并返回下一个完整标记.
29.String next(Pattern pattern)
如果与指定的模式匹配,则返回下一个标记.
30.String next(String pattern)
如果下一个标记与从指定字符串构造的模式匹配,则返回下一个标记.
31.BigDecimal nextBigDecimal()
将输入的下一个标记扫描为 BigDecimal.
32.BigInteger nextBigInteger()
扫描下一个令牌输入的 BigInteger.
33.BigInteger nextBigInteger(int radix)
将输入的下一个标记扫描为 BigInteger.
34. boolean nextBoolean()
将输入的下一个标记扫描为布尔值.
35.byte nextByte()
将输入的下一个标记扫描为字节.
36.byte nextByte(int radix)
将输入的下一个标记扫描为字节.
37.double nextDouble()
将输入的下一个标记扫描为双精度.
38.float nextFloat()
将输入的下一个标记扫描为一个浮点数.
39.int nextInt()
扫描输入的下一个标记为 int.
40.int nextInt(int radix)
将输入的下一个标记扫描为 int.
41.String nextLine()
使扫描器前进到当前行并返回跳过的输入.
42.long nextLong()
将输入的下一个标记扫描为 long.
43.long nextLong(int radix)
将输入的下一个标记扫描为 long.
44.short nextShort()
将输入的下一个标记扫描为 short.
45.short nextShort(int radix)
将输入的下一个标记扫描为短字符.
46.int radix()
返回Scanner的默认基数.
47.void remove ()
Iterator的实现不支持remove操作.
48.Scanner reset()
重置Scanner.
49.Scanner跳过(Pattern pattern)
跳过与指定模式匹配的输入,忽略分隔符.
50.Scannner skip(String pattern)
跳过与从指定字符串构造的模式匹配的输入.
51.String toString()
返回Scanner的字符串表示形式.
52.Scanner useDelimiter(Pattern pattern)
将Scanner的分隔模式设置为指定模式.
53.ScanneruseDelimiter(String pattern)
将Scanner的分隔模式设置为由指定字符串构造的模式.
54.ScanneruseLocale(Locale locale)
将Scanner的区域设置设置为指定的区域设置.
55.Scanner useRadix(int radix)
将Scanner的默认基数设置为指定基数.

继承的方法

该类继承了以下类的方法:

  • java.lang.Object