KeywordFilter.java: import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; public class KeywordFilter { private static Pattern pattern = null; private static KeywordFilter filter = new KeywordFilter(); public static KeywordFilter getInstance() { return (null == filter)?new KeywordFilter():filter; } private KeywordFilter() { init(); } private void init() { StringBuilder stringBuilder = new StringBuilder(); try { InputStream in = KeywordFilter.class .getResourceAsStream("keyword.properties"); Properties pro = new Properties(); //Thread.sleep(5000); pro.load(in); Enumeration<?> enu = pro.propertyNames(); while (enu.hasMoreElements()) { stringBuilder.append(enu.nextElement() + "|"); } stringBuilder.deleteCharAt(stringBuilder.length() - 1); pattern = Pattern.compile(new String(stringBuilder.toString() .getBytes("ISO-8859-1"), "UTF-8")); } catch (Exception ex) { ex.printStackTrace(); } } public String doFilter(String str) { Matcher m = pattern.matcher(str); str = m.replaceAll("***"); return str; } } keyword.properties文件放在同一目录下,其内容就是要屏蔽的词语的集合,每个词语用“|”分割 如下: AV女优|暴力