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

java 文本关键字查找功能原理和代码实现原理 在使用 String 中 indexOf 方法的时候 我们知道如果要是传入一个子字符串作为参数的话类似 from 则这个方法就返回此 from 子字符串第一次在此字符串中出现的位置 即返回此字符串中第一个 from 子字符串中字符 f 的位置 对于此方法忽然有点兴趣 因此我决定查看一下我当前使用的 JDK1 7 中的源码 其核心代码如下 static int

实现原理:

在使用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

package cn.youzi.test;

import java.io.Closeable;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.LineNumberReader;

/**

* 对文本文件的关键词进行搜索

* @author Abel

*

*/

public class TextFileSearch {

public void SearchKeyword(File file,String keyword) {

//参数校验

verifyParam(file, keyword);

//行读取

LineNumberReader lineReader = null;

try {

lineReader = new LineNumberReader(new FileReader(file));

String readLine = null;

while((readLine =lineReader.readLine()) != null){

//判断每一行中,出现关键词的次数

int index = 0;

int next = 0;

int times = 0;//出现的次数

//判断次数

while((index = readLine.indexOf(keyword,next)) != –1) {

next = index + keyword.length();

times++;

}

if(times > 0) {

System.out.println(“第”+ lineReader.getLineNumber() +“行” + “出现 “+keyword+” 次数: “+times);

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

//关闭流

close(lineReader);

}

}

/**

* 参数校验

*

*

* Date: 2014年11月5日

*/

private void verifyParam(File file, String keyword) {

//对参数进行校验证

if(file == null ){

throw new NullPointerException(“the file is null”);

}

if(keyword == null || keyword.trim().equals(“”)){

throw new NullPointerException(“the keyword is null or \”\” “);

}

if(!file.exists()) {

throw new RuntimeException(“the file is not exists”);

}

//非目录

if(file.isDirectory()){

throw new RuntimeException(“the file is a directory,not a file”);

}

//可读取

if(!file.canRead()) {

throw new RuntimeException(“the file can’t read”);

}

}

/**

* 关闭流

*

* Date: 2014年11月5日

*/

private void close(Closeable able){

if(able != null){

try {

able.close();

} catch (IOException e) {

e.printStackTrace();

able = null;

}

}

}

}

二 调用

[java]
view plain
copy

package cn.youzi.test;

import java.io.File;

public class TextFileSearchTest {

public static void main(String[] args) {

TextFileSearch search = new TextFileSearch();

search.SearchKeyword(new File(“E:\\testDir\\news.txt”), “中国”);

}

}



结果 为:

[plain]
view plain
copy

第3行出现 中国 次数: 3

第5行出现 中国 次数: 4

第7行出现 中国 次数: 1

第9行出现 中国 次数: 3

第19行出现 中国 次数: 1

第34行出现 中国 次数: 1

第42行出现 中国 次数: 1

编程小号
上一篇 2025-03-03 11:11
下一篇 2025-02-25 07:51

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/125931.html