统计文件中字符串出现的频率

来源:互联网 发布:little mac 唇膏 编辑:程序博客网 时间:2024/04/28 12:16
public int countString(String word,String fileName) throws Exception{

int count=0;

FileReader fr=new FileReader(fileName);
BufferedReader br=new BufferedReader(fr);
String line=null;
while ((line = br.readLine()) != null) {
            int index = -1;
            while (line.length() >= word.length() && (index = line.indexOf(word)) >= 0) {
                count++;
                line = line.substring(index + word.length());
            }
}
return count;
}
1 0