统计一文件中出现给定字符串的次数
2015-10-16 16:49
387 查看
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class countWordInFile { public static void main(String[] args) { System.out.println(counts()); } public static int counts() { BufferedReader br = null; FileReader fr = null; File file = null; int count = 0; try { file = new File("c:/testbwbr.txt"); fr = new FileReader(file); br = new BufferedReader(fr); String linestr = null; String word = "ee"; while((linestr = br.readLine()) != null) { int index = -1; while(linestr.length() >= word.length() && (index = linestr.indexOf(word))>=0) { count++; linestr = linestr.substring(index + word.length()); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } return count; } }
一行当中可能有多个符合,所以需要双重循环。
相关文章推荐
- C#实现对指定文件夹中文件按修改时间排序
- log4Net使用 z
- go channel
- Qt 改变图片大小
- 用记忆化搜索的方法做01背包题
- Festo控制器--上传文件
- DirectShow Samples
- SpringMVC 介绍以及IDE插件安装配置
- 数据库中间件扩展 MySQL 集群:kingshard
- 貌似太久没来更博呢。。。
- c++定义命名空间
- 1000~2000年闰年的输出(初)
- Intellij14搭建Maven的SpringMVC记录
- 链表与哈希表基本概念及Java常用集合
- 使用 CasperJS 进行简单的 UI 测试
- 创建应用时,对 androidannotations 的配置(Eclipse版本)
- java第六天 宿舍网坏了断更了 今天补上吧 for循环还有各种变态作业
- android内存机制
- PHP函数之error_reporting()详细说明
- 循序渐进了解Kerberos认证工作原理