Java 敏感字符处理类,功能非常强大

敏感字符的处理,性能非常好,采用文件的方式,可通过代码增加敏感词等强大的功能

在开源中国的基础上增加部分方法

依赖apache的io 和lang包

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("傻逼");
    }
	
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值