java比对txt文件,如果有敏感词则返回1否则返回0

zy1314922 2014-10-24 10:33:53
java比对txt文件,如果有敏感词则返回1否则返回0

[size=12px]begin...
1> java 读取 txt 文件
2>servlet 传入一个字符串
3> 比对字符串是否在 txt文件中出现过
4>如果出现则返回给 1
5>否则返回 0
end...
[/size]

请给出代码! 谢谢!
在线等!
...全文
213 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy1314922 2014-10-24
  • 打赏
  • 举报
回复
一行一个词 大概2600多行 我现在大概弄成这样了 但是不知道返回值最后怎么传到servlet去 public class KeywordFilterUtil{ private static KeywordFilterUtil instance; private static List<String> cont; public static synchronized KeywordFilterUtil getIntance() { if (instance == null) { instance = new KeywordFilterUtil(); } return instance; } public static KeyWordResult KeyWordCheck(String key) { return checkWord(key); } private static KeyWordResult checkWord(String key) { KeyWordResult kwr = new KeyWordResult(); kwr.setStatus("1"); kwr.setKeyword(key); try { if(cont==null){ //读取文件 File file = FileUtils.getFileForClassPath("flier.txt"); BufferedReader bReader = null; FileInputStream inFile = null; String line = null; cont = new ArrayList<String>(); inFile = new FileInputStream(file); bReader = new BufferedReader(new InputStreamReader(inFile, "GBK")); while (true) { line = bReader.readLine(); if (line == null) { break; } cont.add(line); } } //比对敏感词如果出现则替换为*号 for (String string : cont) { if (key.contains(string)) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < string.length(); i++) { sb.append("*"); } key = key.replace(string, sb.toString()); kwr.setStatus("0"); kwr.setKeyword(key); } else { kwr.setKeyword(key); } } } catch (Exception e) { e.printStackTrace(); } return kwr; } }
tony4geek 2014-10-24
  • 打赏
  • 举报
回复
不知道内容对不,不多的话,你indexof 或者正则match 下。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧