java.util.Scanner类的close()方法关闭已打开的扫描器。如果扫描仪已经关闭,则在调用此方法时将无效。
用法:
public void close()
返回值:该函数不返回任何值。
以下示例程序旨在说明上述函数:
示例1:
// Java program to illustrate the
// close() method of Scanner class in Java
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Get the string
String s = "Geeksforgeeks has Scanner Class Methods";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print the next line of the string
System.out.println("Scanner: "
+ scanner.nextLine());
// close the scanner
scanner.close();
System.out.println("\nScanner Closed.\n");
System.out.println("Trying to use scanner"
+ " after closing.");
// print the next line of the string
System.out.println(scanner.nextLine());
}
catch (Exception e) {
System.out.println("Exception thrown:\n" + e);
}
}
}
输出:
Scanner: Geeksforgeeks has Scanner Class Methods Scanner Closed. Trying to use scanner after closing. Exception thrown: java.lang.IllegalStateException: Scanner closed
参考: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()
相关用法
- Java Writer close()用法及代码示例
- Java StringWriter close()用法及代码示例
- Java ByteArrayInputStream close()用法及代码示例
- Java CharArrayReader close()用法及代码示例
- Java CharArrayWriter close()用法及代码示例
- Java PrintStream close()用法及代码示例
- Java PushbackReader close()用法及代码示例
- Java Reader close()用法及代码示例
- Java StringReader close()用法及代码示例
- Java PrintWriter close()用法及代码示例
- Java ObjectInputStream close()用法及代码示例
- Java Formatter close()用法及代码示例
- Java Scanner hasNextDouble()用法及代码示例
- Java Scanner nextShort()用法及代码示例
- Java Scanner hasNextBoolean()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Scanner close() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。