package com.ynxbd.common.helper.cache; import com.ynxbd.common.helper.common.FileHelper; import java.util.List; // 敏感词汇过滤 public class TextCache { // 缓存 private static List textList; private synchronized static void getTextList() { if (textList == null) { textList = FileHelper.readLineToList("wx-text.text"); } } public static String textFilter(String text) { if (text == null || "".equals(text)) { return null; } if (textList == null) { // 初始化 getTextList(); } if (textList == null) { return text; } if (textList.size() == 0) { return text; } StringBuilder sb; int len; for (String s : textList) { len = s.length(); if (text.contains(s)) { if (len > 3) { sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.append("*"); } text = text.replaceAll(s, "*"); } else { if (len == 1) { text = text.replaceAll(s, "*"); } else if (len == 2) { text = text.replaceAll(s, "**"); } else if (len == 3) { text = text.replaceAll(s, "***"); } } } } return text; } public static void main(String[] args) { String s = textFilter("批次编号:20043945妇科材料库“一次性使用无菌阴道扩张器中号A型轴转式”的库存为2已不足执行2个过程失败!"); System.out.println(s); } }