Java敏感字符串处理工具类(河蟹专用)
2016-04-29 10:12
381 查看
敏感字符的处理,性能非常好,采用文件的方式,可通过代码增加敏感词等强大的功能
依赖apache的io 和lang包,惯例还是直接code吧:
依赖apache的io 和lang包,惯例还是直接code吧:
package com.wiker; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.lang.StringUtils; /** * 敏感字词处理类 * @author Wiker * @date 2010-1-11 下午10:51:30 */ public class BadWord { private final static File wordfilter = new File("C:/wordfilter.txt"); private static long lastModified = 0L; private static List<String> words = new ArrayList<String>(); private static void checkReload(){ if(wordfilter.lastModified() > lastModified){ synchronized(BadWord.class){ try{ lastModified = wordfilter.lastModified(); LineIterator lines = FileUtils.lineIterator(wordfilter, "utf-8"); while(lines.hasNext()){ String line = lines.nextLine(); if(StringUtils.isNotBlank(line)) words.add(StringUtils.trim(line).toLowerCase()); } }catch(IOException e){ e.printStackTrace(); } } } } /** * 检查敏感字内容 * @param contents */ public static String check(String ...contents) { if(!wordfilter.exists()) return null; checkReload(); for(String word : words){ for(String content : contents) if(content!=null && content.indexOf(word) >= 0) return word; } return null; } /** * 检查字符串是否包含敏感词 * * @param content * @return */ public static boolean isContain(String content) { if(!wordfilter.exists()) return false; checkReload(); for(String word : words){ if(content!=null && content.indexOf(word) >= 0) return true; } return false; } /** * 替换掉字符串中的敏感词 * * @param str 等待替换的字符串 * @param replaceChar 替换字符 * @return */ public static String replace(String str,String replaceChar){ checkReload(); for(String word : words){ if(str.indexOf(word)>=0){ String reChar = ""; for(int i=0;i<word.length();i++){ reChar += replaceChar; } str = str.replaceAll(word, reChar); } } return str; } public static List<String> lists() { checkReload(); return words; } /** * 添加敏感词 * * @param word * @throws IOException */ public static void add(String word) throws IOException { word = word.toLowerCase(); if(!words.contains(word)){ words.add(word); FileUtils.writeLines(wordfilter, "UTF-8", words); lastModified = wordfilter.lastModified(); } } /** * 删除敏感词 * * @param word * @throws IOException */ public static void delete(String word) throws IOException { word = word.toLowerCase(); words.remove(word); FileUtils.writeLines(wordfilter, "UTF-8", words); lastModified = wordfilter.lastModified(); } public static void main(String[] args) throws Exception{ System.out.println(BadWord.replace("中国共产党钓鱼岛","*")); System.out.println(BadWord.isContain("岛")); BadWord.add("傻逼"); } }
相关文章推荐
- java web系统优化, mysql查询优化
- JAVA的newInstance()和new的区别(JAVA反射机制,通过类名来获取该类的实例化对象)
- JavaMail
- Java图片工具类完成图片的截取和任意缩放
- Java学习之旅基础知识篇:数据类型及流程控制
- 用Eclipse打开已有工程
- Java 8 接口里的默认方法特性研究
- JAVA回调机制(CallBack)详解
- java程序的工作原理
- 正则表达式知识详解之前后查找(环视) (java版示例)
- Java Volatile关键字和 Synchronized关键字的区别
- eclipse安装flex插件
- Java学习之旅开篇:运行机制及环境搭建
- JAVA char与byte的区别和转换
- Rxjava基础自己总结的有不足欢迎探讨
- njupt Java 实验1
- Java 多态
- java.lang.String的intern()方法
- java 代码
- 非常经典的JAVA编程题(水仙花数)