//输出查询结果
if (ans != null) {
System.out.println("字库中关键词个数:"+ cnt);//字库中关键词个数
System.out.println("脏字库内容如下:"+ ans);
} else {
System.out.println("没有可以匹配的信息");
//输出脏字库中的内容
}
}
//得到指定路径文件中的内容
private static String getcontent(String filepath) {
String all = "";
File file = new File(filepath);
try {
if (!file.isFile()) {
System.out.println("文件路径不对,请修改路径");
} else {
File readfile = new File(filepath);
BufferedReader br = new BufferedReader(new FileReader(readfile));
String ss = br.readLine();
while (ss != null) {
all = all + ss;
//all中存放读取的文件内容信息
ss = br.readLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return all;
}
//在待测文件中匹配脏字出现的次数
private static int searchkeyword(String[] str, int cnt, String filepath){
int number = 0;
String s = "";
s = getcontent(filepath);
for (int i = 0; i < cnt; i++) {
if (s.indexOf(str) > -1) {
number++;
}
}
return number;
}
至此,完成了脏字过滤器软件代码的编写工作,接下来可以进行run操作,即可以得到待测文件库中的待测文件包含脏字次数及出处等相关信息的结果。