简体   繁体   English

何时以及为何使用Scanner?

[英]When and why to use Scanner?

I am new to Java and was reading different ways to handle input and output. 我是Java的新手,正在阅读处理输入和输出的不同方法。 From what I've read, the various strategies boil down to two things ie 根据我的阅读,各种策略可以归结为两件事,即

  1. Reading / Writing by byte ( FileInputStream , FileOutputStream ). 按字节读取/写入( FileInputStreamFileOutputStream )。
  2. Reading / Writing by character ( FileReader , FileWriter ). 按字符读取/写入( FileReaderFileWriter )。

But where does Scanner fit into the picture? 但是Scanner在图片中适合什么位置? An example use case would be good. 一个示例用例会很好。

Use a Scanner if you want to read tokens 如果您想读取令牌,请使用扫描仪

Use a FileReader when reading streams of characters 读取字符流时使用FileReader

Use a FileInputReader when reading binary files. 读取二进制文件时,请使用FileInputReader

Scanner: 扫描器:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. 扫描程序使用定界符模式将其输入分为令牌,默认情况下,该模式与空格匹配。 The resulting tokens may then be converted into values of different types using the various next methods. 然后,可以使用各种下一种方法将生成的令牌转换为不同类型的值。 Scanner - JavaDoc 扫描仪-JavaDoc

And for Filereader and FileInputReader: 对于Filereader和FileInputReader:

FileReader is meant for reading streams of characters. FileReader用于读取字符流。 For reading streams of raw bytes, consider using a FileInputStream. 为了读取原始字节流,请考虑使用FileInputStream。 FileReader -javadoc FileReader -javadoc

Example for a scanner: 扫描仪示例:

String input = "1 fish 2 fish red fish blue fish";
   Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
   System.out.println(s.nextInt());
   System.out.println(s.nextInt());
   System.out.println(s.next());
   System.out.println(s.next());
 s.close(); 

Scanner is a higher level class used to read input by tokens. 扫描程序是用于读取令牌输入的更高级别的类。

What is a token? 什么是代币? For the class Scanner a token can be a primitive value and some special object: 对于Scanner类,令牌可以是原始值和某些特殊对象:

  • long (with nextLong) 长(带有nextLong)
  • int (with nextInt) int(与nextInt)
  • short (with nextShort) 短(与nextShort)
  • byte (with nextByte) 字节(带有nextByte)
  • boolean (with nextBoolean) 布尔值(带有nextBoolean)
  • BigDecimal (with nextBigDecimal) BigDecimal(带有nextBigDecimal)
  • float (with nextFloat) 浮动(与nextFloat)
  • double (with nextDouble) 双(与nextDouble)

In addition it is possible to read a whole line with nextLine . 另外,可以使用nextLine读取整行。

The Scanner class is a class in java.util which allows the user to read values of various types. Scanner类是java.util的类,它允许用户读取各种类型的值。 There are two constructors that are particularly useful: one takes an InputStream object as a parameter and the other takes a FileReader object as a parameter. 有两个特别有用的构造函数:一个将InputStream对象作为参数,另一个将FileReader对象作为参数。

Scanner in = new Scanner(System.in);  // System.in is an InputStream
Scanner inFile = new Scanner(new FileReader("myFile"));// If the file ≥myFile≤ is not found, a FileNotFoundException is thrown. 

Various Methods: 各种方法:

  • int nextInt() : Returns the next token as an int. int nextInt() :将下一个标记作为int返回。 If the next token is not an integer, InputMismatchException is thrown. 如果下一个标记不是整数,则抛出InputMismatchException。
  • long nextLong() : Returns the next token as a long. long nextLong() :将下一个标记作为long返回。 If the next token is not an integer, InputMismatchException is thrown. 如果下一个标记不是整数,则抛出InputMismatchException。
  • float nextFloat() : Returns the next token as a float. float nextFloat() :以float形式返回下一个标记。 If the next token is not a float or is out of range, InputMismatchException is thrown. 如果下一个令牌不是浮点型或超出范围,则抛出InputMismatchException。
  • double nextDouble() : Returns the next token as a long. double nextDouble() :以长double nextDouble()返回下一个标记。 If the next token is not a float or is out of range, InputMismatchException is thrown. 如果下一个令牌不是浮点型或超出范围,则抛出InputMismatchException。
  • String next(): Finds and returns the next complete token from this scanner and returns it as a string; String next():查找并返回此扫描器的下一个完整令牌,并将其作为字符串返回; a token is usually ended by whitespace such as a blank or line break. 令牌通常以空格结尾,例如空格或换行符。 If not token exists, NoSuchElementException is thrown. 如果不存在令牌,则抛出NoSuchElementException。
  • String nextLine() : Returns the rest of the current line, excluding any line separator at the end. String nextLine() :返回当前行的其余部分,但不包括末尾的任何行分隔符。
  • void close() : Closes the scanner. void close() :关闭扫描仪。

The Scanner looks for tokens in the input. 扫描程序在输入中查找令牌。 A token is a series of characters that ends with what Java calls whitespace. 令牌是一系列以Java称为空白结尾的字符。 Thus, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token. 因此,如果我们读取一行包含一系列用空格分隔的数字的行,则扫描程序将把每个数字作为一个单独的标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM