JAVA 搜索文本文件中的关键字

原文链接:http://blog.csdn.net/blog_abel/article/details/40858245


用JAVA实现对文本文件中的关键字进行搜索, 依据每一行,得到每一行中出现关键词的个数。使用java.io.LineNumberReader.java 进行行读取。示例如下:

一 实现类

[java]  view plain  copy
  1. package cn.youzi.test;  
  2.   
  3. import java.io.Closeable;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.LineNumberReader;  
  8.   
  9. /** 
  10.  * 对文本文件的关键词进行搜索 
  11.  * @author Abel 
  12.  * 
  13.  */  
  14. public class TextFileSearch {  
  15.   
  16.     public void SearchKeyword(File file,String keyword) {  
  17.         //参数校验  
  18.         verifyParam(file, keyword);  
  19.           
  20.         //行读取  
  21.         LineNumberReader lineReader = null;  
  22.         try {  
  23.             lineReader = new LineNumberReader(new FileReader(file));  
  24.             String readLine = null;  
  25.             while((readLine =lineReader.readLine()) != null){  
  26.                 //判断每一行中,出现关键词的次数  
  27.                 int index = 0;  
  28.                 int next = 0;  
  29.                 int times = 0;//出现的次数  
  30.                 //判断次数  
  31.                 while((index = readLine.indexOf(keyword,next)) != -1) {  
  32.                     next = index + keyword.length();  
  33.                     times++;  
  34.                 }  
  35.                 if(times > 0) {  
  36.                     System.out.println("第"+ lineReader.getLineNumber() +"行" + "出现 "+keyword+" 次数: "+times);  
  37.                 }  
  38.             }  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         } finally {  
  42.             //关闭流  
  43.             close(lineReader);  
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * 参数校验 
  49.      *  
  50.      * <br> 
  51.      * Date: 2014年11月5日 
  52.      */  
  53.     private void verifyParam(File file, String keyword) {  
  54.         //对参数进行校验证  
  55.         if(file == null ){  
  56.             throw new NullPointerException("the file is null");  
  57.         }  
  58.         if(keyword == null || keyword.trim().equals("")){  
  59.             throw new NullPointerException("the keyword is null or \"\" ");  
  60.         }  
  61.           
  62.         if(!file.exists()) {  
  63.             throw new RuntimeException("the file is not exists");  
  64.         }  
  65.         //非目录  
  66.         if(file.isDirectory()){  
  67.             throw new RuntimeException("the file is a directory,not a file");  
  68.         }  
  69.           
  70.         //可读取  
  71.         if(!file.canRead()) {  
  72.             throw new RuntimeException("the file can't read");  
  73.         }  
  74.     }  
  75.       
  76.     /** 
  77.      * 关闭流 
  78.      * <br> 
  79.      * Date: 2014年11月5日 
  80.      */  
  81.     private void close(Closeable able){  
  82.         if(able != null){  
  83.             try {  
  84.                 able.close();  
  85.             } catch (IOException e) {  
  86.                 e.printStackTrace();  
  87.                 able = null;  
  88.             }  
  89.         }  
  90.     }  
  91.   
  92. }  

二 调用

[java]  view plain  copy
  1. package cn.youzi.test;  
  2.   
  3. import java.io.File;  
  4.   
  5. public class TextFileSearchTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.   
  9.         TextFileSearch search = new TextFileSearch();  
  10.         search.SearchKeyword(new File("E:\\testDir\\news.txt"), "中国");  
  11.     }  
  12.   
  13. }  

结果 为:

[plain]  view plain  copy
  1. 第3行出现 中国 次数: 3  
  2. 第5行出现 中国 次数: 4  
  3. 第7行出现 中国 次数: 1  
  4. 第9行出现 中国 次数: 3  
  5. 第19行出现 中国 次数: 1  
  6. 第34行出现 中国 次数: 1  
  7. 第42行出现 中国 次数: 1  
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值