Java实现——统计文件中字符串出现次数
来源:互联网 发布:软件采购进度报表 编辑:程序博客网 时间:2024/06/07 10:12
转自 Java面试题全集(上)(http://blog.csdn.net/jackfrued/article/details/44921941/ )第70题
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public final class CountStringUtil { // 工具类中的方法都是静态方式访问的因此将构造器私有不允许创建对象(绝对好习惯),且类定义为final型,不允许被继承 private CountStringUtil(){} /** * 统计给定文件中给定字符串的出现次数 * * @param filename 文件名 * @param word 字符串 * @return 字符串在文件中出现的次数 */ public static int countStringInFile(String filename , String word){ int counter = 0; try{ 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){ counter++; line = line.substring(index + word.length()); } } }catch(IOException e){ e.printStackTrace(); } return counter; }
public static int countString2(String filename , String word){int counter = 0;try{FileReader fr = new FileReader(filename);BufferedReader br = new BufferedReader(fr);String line = null;int l = word.length();while((line = br.readLine()) != null){for(int i = 0 ; i <= line.length() - l; i++){if(line.substring(i, i + l).equals(word)){counter++;}}}}catch(IOException e){e.printStackTrace();}return counter; } public static void main(String[] args) { String filename = "D:\\support\\countword.txt"; String word = "zhuo"; System.out.println(CountStringUtil.countStringInFile(filename, word)); }}
阅读全文
0 0
- Java实现——统计文件中字符串出现次数
- linux统计文件中字符串出现次数
- Java中实现统计一个字符串在另一个字符串中出现的次数统计
- java源码——统计字符串中字符出现的次数
- java基础—统计一个字符串中各个字符出现的次数
- 统计文件中出现字符串的次数(中,英)
- 统计一个字符串在文件中出现的次数
- 统计某个字符串在文件中出现的次数
- 统计一文件中出现给定字符串的次数
- 统计这个字符串在这个文件中出现的次数。
- java统计文本中某个字符串出现的次数
- java统计字符串中各个字符的出现次数
- Java,统计字符串中各字符出现的次数
- java:统计a字符在字符串中出现的次数
- java统计字符串中字母出现的次数
- java统计字符串中每个字符出现的次数
- java统计字符串中单个字符出现的次数
- java统计字符串中子字符串出现次数
- sortedArrayUsingComparator
- RNN循环神经网络
- gcc 用法
- LeetCode c语言-Generate Parentheses和买票找零问题
- 【学习摘记】马士兵bbs初级版_课时14-15_login权限控制:session的典型用法
- Java实现——统计文件中字符串出现次数
- 通过http协议导出excel
- 2017中国容器技术使用情况有奖调查
- WebSocket 学习(五)--用socketIO实现聊天室
- TensorFlow
- DataBinding基本使用(4)
- 六个创建型模式1:简单工厂模式-Simple Factory Pattern【学习难度:★★☆☆☆,使用频率:★★★☆☆】
- 使用Activity直接打开其他应用的方法
- http和https区别