📜  Java I/O-Scanner

📅  最后修改于: 2020-09-27 07:40:28             🧑  作者: Mango

Java Scanner

Java的Scanner类可在java.util包中找到。 Java提供了多种从键盘读取输入的方法,java.util.Scanner类就是其中之一。

Java Scanner类使用定界符将输入分为令牌,该定界符默认为空格。它提供了许多读取和解析各种原始值的方法。

Java Scanner类广泛用于使用正则表达式解析文本以获取字符串和原始类型。这是在Java中获取输入的最简单方法。借助于Java中的Scanner,我们可以从用户那里获取基本类型的输入,例如int,long,double,byte,float,short等。

Java Scanner类扩展了Object类,并实现了Iterator和Closeable接口。

Java Scanner类提供nextXXX()方法以返回值的类型,例如nextInt(),nextByte(),nextShort(),next(),nextLine(),nextDouble(),nextFloat(),nextBoolean()等。要从扫描仪获取单个字符,可以调用next()。charAt(0)方法,该方法返回单个字符。

Java扫描器类声明

public final class Scanner
      extends Object
      implements Iterator 

如何获取Java扫描仪

要获取从用户读取输入的Java Scanner实例,我们需要在Scanner类的构造函数中传递输入流(System.in)。例如:

Scanner in = new Scanner(System.in);

要获取解析字符串的Java Scanner实例,我们需要在Scanner类的构造函数中传递字符串。例如:

Scanner in = new Scanner("Hello Javatpoint");

Java扫描器类构造函数

SN Constructor Description
1) Scanner(File source) It constructs a new Scanner that produces values scanned from the specified file.
2) Scanner(File source, String charsetName) It constructs a new Scanner that produces values scanned from the specified file.
3) Scanner(InputStream source) It constructs a new Scanner that produces values scanned from the specified input stream.
4) Scanner(InputStream source, String charsetName) It constructs a new Scanner that produces values scanned from the specified input stream.
5) Scanner(Readable source) It constructs a new Scanner that produces values scanned from the specified source.
6) Scanner(String source) It constructs a new Scanner that produces values scanned from the specified string.
7) Scanner(ReadableByteChannel source) It constructs a new Scanner that produces values scanned from the specified channel.
8) Scanner(ReadableByteChannel source, String charsetName) It constructs a new Scanner that produces values scanned from the specified channel.
9) Scanner(Path source) It constructs a new Scanner that produces values scanned from the specified file.
10) Scanner(Path source, String charsetName) It constructs a new Scanner that produces values scanned from the specified file.

Java Scanner类方法

以下是扫描仪方法的列表:

SN Modifier & Type Method Description
1) void close() It is used to close this scanner.
2) pattern delimiter() It is used to get the Pattern which the Scanner class is currently using to match delimiters.
3) Stream findAll() It is used to find a stream of match results that match the provided pattern string.
4) String findInLine() It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
5) string findWithinHorizon() It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
6) boolean hasNext() It returns true if this scanner has another token in its input.
7) boolean hasNextBigDecimal() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
8) boolean hasNextBigInteger() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextBigDecimal() method or not.
9) boolean hasNextBoolean() It is used to check if the next token in this scanner’s input can be interpreted as a Boolean using the nextBoolean() method or not.
10) boolean hasNextByte() It is used to check if the next token in this scanner’s input can be interpreted as a Byte using the nextBigDecimal() method or not.
11) boolean hasNextDouble() It is used to check if the next token in this scanner’s input can be interpreted as a BigDecimal using the nextByte() method or not.
12) boolean hasNextFloat() It is used to check if the next token in this scanner’s input can be interpreted as a Float using the nextFloat() method or not.
13) boolean hasNextInt() It is used to check if the next token in this scanner’s input can be interpreted as an int using the nextInt() method or not.
14) boolean hasNextLine() It is used to check if there is another line in the input of this scanner or not.
15) boolean hasNextLong() It is used to check if the next token in this scanner’s input can be interpreted as a Long using the nextLong() method or not.
16) boolean hasNextShort() It is used to check if the next token in this scanner’s input can be interpreted as a Short using the nextShort() method or not.
17) IOException ioException() It is used to get the IOException last thrown by this Scanner’s readable.
18) Locale locale() It is used to get a Locale of the Scanner class.
19) MatchResult match() It is used to get the match result of the last scanning operation performed by this scanner.
20) String next() It is used to get the next complete token from the scanner which is in use.
21) BigDecimal nextBigDecimal() It scans the next token of the input as a BigDecimal.
22) BigInteger nextBigInteger() It scans the next token of the input as a BigInteger.
23) boolean nextBoolean() It scans the next token of the input into a boolean value and returns that value.
24) byte nextByte() It scans the next token of the input as a byte.
25) double nextDouble() It scans the next token of the input as a double.
26) float nextFloat() It scans the next token of the input as a float.
27) int nextInt() It scans the next token of the input as an Int.
28) String nextLine() It is used to get the input string that was skipped of the Scanner object.
29) long nextLong() It scans the next token of the input as a long.
30) short nextShort() It scans the next token of the input as a short.
31) int radix() It is used to get the default radix of the Scanner use.
32) void remove() It is used when remove operation is not supported by this implementation of Iterator.
33) Scanner reset() It is used to reset the Scanner which is in use.
34) Scanner skip() It skips input that matches the specified pattern, ignoring delimiters
35) Stream tokens() It is used to get a stream of delimiter-separated tokens from the Scanner object which is in use.
36) String toString() It is used to get the string representation of Scanner using.
37) Scanner useDelimiter() It is used to set the delimiting pattern of the Scanner which is in use to the specified pattern.
38) Scanner useLocale() It is used to sets this scanner’s locale object to the specified locale.
39) Scanner useRadix() It is used to set the default radix of the Scanner which is in use to the specified radix.

例子1

让我们看一个简单的Java扫描仪示例,我们从用户那里获得单个输入。在这里,我们通过in.nextLine()方法请求一个字符串 。

import java.util.*;
public class ScannerExample {
public static void main(String args[]){
          Scanner in = new Scanner(System.in);
          System.out.print("Enter your name: ");
          String name = in.nextLine();
          System.out.println("Name is: " + name);           
          in.close();           
          }
}

输出:

Enter your name: sonoo jaiswal
Name is: sonoo jaiswal

例子2

import java.util.*;
public class ScannerClassExample1 {  
      public static void main(String args[]){                   
          String s = "Hello, This is JavaTpoint.";
          //Create scanner Object and pass string in it
          Scanner scan = new Scanner(s);
          //Check if the scanner has a token
          System.out.println("Boolean Result: " + scan.hasNext());
          //Print the string
          System.out.println("String: " +scan.nextLine());
          scan.close();         
          System.out.println("--------Enter Your Details-------- ");
          Scanner in = new Scanner(System.in);
          System.out.print("Enter your name: ");  
          String name = in.next(); 
          System.out.println("Name: " + name);         
          System.out.print("Enter your age: ");
          int i = in.nextInt();
          System.out.println("Age: " + i);
          System.out.print("Enter your salary: ");
          double d = in.nextDouble();
          System.out.println("Salary: " + d);       
          in.close();         
          }  
}

输出:

Boolean Result: true
String: Hello, This is JavaTpoint.
-------Enter Your Details--------- 
Enter your name: Abhishek
Name: Abhishek
Enter your age: 23
Age: 23
Enter your salary: 25000
Salary: 25000.0

例子3

import java.util.*;
public class ScannerClassExample2 {  
      public static void main(String args[]){                   
          String str = "Hello/This is JavaTpoint/My name is Abhishek.";
          //Create scanner with the specified String Object
          Scanner scanner = new Scanner(str);
          System.out.println("Boolean Result: "+scanner.hasNextBoolean());          
          //Change the delimiter of this scanner
          scanner.useDelimiter("/");
          //Printing the tokenized Strings
          System.out.println("---Tokenizes String---"); 
      while(scanner.hasNext()){
          System.out.println(scanner.next());
      }
          //Display the new delimiter
          System.out.println("Delimiter used: " +scanner.delimiter());          
          scanner.close();
          }  
}

输出:

Boolean Result: false
---Tokenizes String---
Hello
This is JavaTpoint
My name is Abhishek.
Delimiter used: /