package cn.ma;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class WordCount {
//私有的构造方法
private WordCount() {}
//获得对象的静态方法
public static WordCount getInstance() {
return new WordCount();
}
//path:文件路径
// word:要统计的字符
public int wordCount(String path, String word) throws IOException {
int count = 0;
File file = new File(path);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
int index = -1;
while (line.length() >= word.length() && index < line.indexOf(word)) {
count++;
line = line.substring(line.indexOf(word) + word.length());
}
}
return count;
}
}