前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 文本关键字查找功能原理和代码

java 文本关键字查找功能原理和代码

作者头像
全栈程序员站长
发布2022-08-09 14:39:58
1.1K0
发布2022-08-09 14:39:58
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

实现原理:

在使用String中indexOf()方法的时候,我们知道如果要是传入一个子字符串作为参数的话类似”from”,则这个方法就返回此”from”子字符串第一次在此字符串中出现的位置,即返回此字符串中第一个”from”子字符串中字符”f”的位置。

对于此方法忽然有点兴趣,因此我决定查看一下我当前使用的JDK1.7中的源码,其核心代码如下: static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { …… char first = target[targetOffset]; int max = sourceOffset + (sourceCount – targetCount);

for (int i = sourceOffset + fromIndex; i <= max; i++) { /* 查找子字符串的第一个字符,如果第一个字符都没有出现,则此字符串中不包含这个子字符串 */ if (source[i] != first) { while (++i <= max && source[i] != first); }

/* 查找到第一个字符,则继续查找剩下的字符 */ if (i <= max) { int j = i + 1; int end = j + targetCount – 1; for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++);

if (j == end) { /* Found whole string. */ return i – sourceOffset; } } } return -1; }

如上述代码中我写的注释那样,这个方法首先会查找子字符串的头字符在此字符串中第一次出现的位置,再以此位置的下一个位置作为起始,然后将子字符串的字符(头字符的下一个字符开始)依次和此字符串中字符进行比较,如果全部相等,则返回这个头字符在此字符串中的位置;如果有不相等的,则继续在剩下的字符串中查找这个子字符串的头字符,继续进行上面的过程,直到查找到子字符串或没有找到返回-1为止。

代码:

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

一 实现类

[java] view plain copy

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

二 调用

[java] view plain copy

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

结果 为:

[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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106117.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码:
  • 一 实现类
  • 二 调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档