1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import
java.util.Scanner;
import
java.io.*;
public
class
FileScannerTest{
public
static
void
main(String args[]){
//**************Scanner 的一般用
//1.public Scanner(InputStream source),利用InputStream 对象进行构造
Scanner myScanner1 =
new
Scanner(System.in);
while
(myScanner1.hasNextInt()){
int
i=myScanner1.nextInt();
System.out.println(i);
}
//2.public Scanner(File source) throws FileNotFoundException , 利用File对象进行构造
try
{
Scanner myScanner2 =
new
Scanner(
new
File(
"in.txt"
));
while
(myScanner2.hasNextInt()){
int
i=myScanner2.nextInt();
System.out.println(i);
}
}
catch
(FileNotFoundException e){
System.out.println(
"该文件不存在!"
);
}
//3.public Scanner(String str), 利用一个String对象进行构造
Scanner myScanner3 =
new
Scanner(
new
String(
"1 2 3 4 a f f 4 56"
));
//遇到非整数的地方hasNextInt()返回false
while
(myScanner3.hasNextInt()){
int
i=myScanner3.nextInt();
System.out.println(i);
}
}
}
|
本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3817769.html,如需转载请自行联系原作者