Java 敏感字符处理类,功能非常强大
2013-11-19 14:05
316 查看
敏感字符的处理,性能非常好,采用文件的方式,可通过代码增加敏感词等强大的功能
在开源中国的基础上增加部分方法
依赖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("傻逼");
}
}
在开源中国的基础上增加部分方法
依赖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("傻逼");
}
}
相关文章推荐
- 功能非常强大的图片处理软件。graphicsmagick
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- 字串处理 expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
- 容易忽视但功能能非常强大的javaAPI
- javaWed项目中用过滤器实现转码功能,敏感词汇过滤更能,处理Get和Post接收数据中的中文乱码问题以及敏感词汇的处理
- Windows批处理功能配合脚本还是很强大的!(1)
- Android JNI开发高级篇有关Android JNI开发中比较强大和有用的功能就是从JNI层创建、构造Java的类或执行Java层的方法获取属性等操作。 一、类的相关操作 1. jclass FindClass(JNIEnv *env, const char *name);
- 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理
- Java不可见字符处理
- jsp 中 js变量引用java变量时 特殊字符的处理
- 20个开发人员非常有用的Java功能代码
- 处理java错误”编码 GBK 的不可映射字符“
- 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
- java: 功能强大的分页管理器(PageManage)
- java利用DFA算法实现敏感词过滤功能
- Java实现MD5算法(原来有这么强大的功能)
- http请求数据包含特殊字符处理(java)
- 20个开发人员非常有用的Java功能代码