Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65598
  • 博文数量: 11
  • 博客积分: 276
  • 博客等级: 二等列兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 11:14
文章分类

全部博文(11)

文章存档

2012年(11)

我的朋友
最近访客

分类: Java

2012-02-17 12:52:46

import java.io.IOException;
  import java.io.InputStream;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Enumeration;
  import java.util.Properties;
  import java.util.regex.Pattern;
  import java.util.regex.Matcher;
  public class KeyWordFilter
  {
  private static Pattern pattern = null;
  // 从words.properties初始化正则表达式字符串
  private static void initPattern()
  {
   StringBuffer patternBuf = new StringBuffer("");
   try
   {
   InputStream in = KeyWordFilter.class.getClassLoader().getResourceAsStream("words.properties");
   Properties pro = new Properties();
   pro.load(in);
   Enumeration enu = pro.propertyNames();
   patternBuf.append("(");
   while(enu.hasMoreElements())
   {
   patternBuf.append((String)enu.nextElement()+"|");
   }
   patternBuf.deleteCharAt(patternBuf.length()-1);
   patternBuf.append(")");
  
  //unix换成UTF-8
   //pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "UTF-8"));
  //win下换成gb2312
   pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312"));
   }
   catch(IOException ioEx)
   {
   ioEx.printStackTrace();
   }
  }
  private static String doFilter(String str)
  {
   Matcher m = pattern.matcher(str);
   str = m.replaceAll("");
   return str;
  }
  public static void main(String[] args)
  {
   String str = "国敏感词一院学位办就敏感词三的报道表示敏感词二";
   System.out.println("str:"+str);
   initPattern();
   Date d1 = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss:SSS Z");
   System.out.println("start:"+formatter.format(d1));
   System.out.println("共"+str.length()+"个字符,查到" + KeyWordFilter.doFilter(str));
   Date d2 = new Date();
   System.out.println("end:"+formatter.format(d2));
  }
  }
  输出为:
  __________________________________
  str:国敏感词一院学位办就敏感词三的报道表示敏感词二
  start:星期二, 24 三月 2009 14:50:17:171 +0800
  共23个字符,查到国院学位办就的报道表示
阅读(1294) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~