import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("请输入文件名:");
String fileName = input.next();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
System.out.println("请输入要查找的字符串:");
String find = input.next();
long count = 0;
String line = br.readLine();
while(null != line) {
count += count(line,find);
line = br.readLine();
}
System.out.println("文件" + fileName + "中共有" + count +"个" + find);
br.close();
}
/**
* 在str中查找find,并返回找到的个数
* @param str 从文件读入的一行
* @param find 要查找的字符串
* @return str中find的个数
*/
private static long count(String str,String find) {
long count = 0;
int len = find.length();
int index = 0;
for(int i=0; i
if((index = str.indexOf(find, i)) > -1)
count ++;
else
break;
}
return count;
}
}