解决方案 »

  1.   

    不知道内容对不,不多的话,你indexof 或者正则match 下。
      

  2.   

    一行一个词
    大概2600多行我现在大概弄成这样了 但是不知道返回值最后怎么传到servlet去public class KeywordFilterUtil{
    private static KeywordFilterUtil instance; private static List<String>  cont;
    public static synchronized KeywordFilterUtil getIntance() {
    if (instance == null) {
    instance = new KeywordFilterUtil();
    }
    return instance;
    } public static KeyWordResult KeyWordCheck(String key) {
    return checkWord(key);
    } private static KeyWordResult checkWord(String key) {
    KeyWordResult kwr = new KeyWordResult();
    kwr.setStatus("1");
    kwr.setKeyword(key);

    try {

    if(cont==null){
    //读取文件
    File file = FileUtils.getFileForClassPath("flier.txt");
    BufferedReader bReader = null;
    FileInputStream inFile = null;
    String line = null;
    cont = new ArrayList<String>();
    inFile = new FileInputStream(file);
    bReader = new BufferedReader(new InputStreamReader(inFile, "GBK"));
    while (true) {
    line = bReader.readLine();
    if (line == null) {
    break;
    }
    cont.add(line);
    }
    }
    //比对敏感词如果出现则替换为*号
    for (String string : cont) {
    if (key.contains(string)) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < string.length(); i++) {
    sb.append("*");
    }
    key = key.replace(string, sb.toString());

    kwr.setStatus("0");
    kwr.setKeyword(key);
    } else {
    kwr.setKeyword(key);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return kwr;
    }
    }