代码
import java.io.*;
import java.util.Scanner;
public class Fileread {
public static void main(String[] args) throws IOException {
File file = new File("List.txt");
String country="india";
int count=countWord(country,file);
System.out.println("The number of students from"+country+" is "+count);
}
public static int countWord(String word, File file) {
int count = 0;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(word))
count++;
}
return count;
}
}
错误:
Exception in thread "main" java.io.FileNotFoundException: List.txt
(The system cannot find the file specified) at
java.io.FileInputStream.open(Native Method) at
java.io.FileInputStream.<init>(Unknown Source) at
java.util.Scanner.<init>(Unknown Source) at
random.Fileread.countWord(Fileread.java:25) at
random.Fileread.main(Fileread.java:17)