Costumo ver o Post Bar bloqueando o conteúdo de postagem de vários usuários e pensei em como implementar isso. Pensei em usar strings para substituí -lo para implementá -lo, mas essa eficiência é muito baixa e não pode garantir a correspondência mais longa. Esta é a minha ideia inicial naquele momento. Em um projeto em que trabalhei recentemente, precisava bloquear algum conteúdo e analisei esse problema novamente e finalmente formei o código a seguir.
/ ***@Descrição: implementação da função de palavra mascarada*/ pacote cn.yicha.novel.search.util; importar java.io.bufferedReader; importar java.io.file; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.ioException; importar java.io.inputStreamReader; importar java.util.hashset; importar cn.yicha.novel.search.config.config; classe pública proibido {privado privado estático proibido = new proibidden (); // Mask Word Hashset Hashset Private <String> keystring = new HashSet <String> (); private final static int maxlength = caractere.max_value; // máscara de comprimento da palavra HASHSET Array @suppresswarnings ("desmarcado") Hashset privado <TEGER> [] keyLength = new HashSet [maxLength]; privado proibidden () {loadforbidden (config.getclassroot () + "proibidden.txt"); } public static proibidden getforbidden () {return proibidden; } / ** * @param str * @return * @description: a sequência de entrada é mascarada para obter o comprimento máximo correspondente * / public string read (string str) {if (str == null) {return null; } StringBuffer stringBuffer = new StringBuffer (); int start = 0; for (int i = 0; i <str.Length ();) {int at = str.Charat (i); if (keyLength [at] == null) {i ++; continuar; } else {int ml = 0; for (objeto obj: keyLength [at] .toarray ()) {int len = ((inteiro) obj) .IntValue (); if (i + len <= str.Length ()) {string s = str.substring (i, i + len); if (keystring.contains (s)) {// Comprimento máximo correspondente a ml = len> ml? Len: ML; }}} if (ml> 0) {stringbuffer.append (str.substring (start, i)). append ("***"); i += ml; start = i; } else {i ++; }}}} if (start <str.length ()) {stringbuffer.append (str.substring (start)); } return stringbuffer.toString (); } /** * @Param Path * @Description: Inicialize as palavras de mascaramento de carregamento * @Description: A lógica do formato de dados para armazenar palavras de mascaramento é a seguinte * @Description: Construa um hashset <String> para armazenar todas as palavras de mascaramento * @Description: construir um hashset <Teger> Array com comprimento value, that is, the position where the relevant information is stored in the array, * @Description: If "Hello" gets the int value of 'you' is 20320, that is, the relevant information of "Hello" is stored in the 20320 position of the array* @Description: The length of the mask word stored in each bit of the array is stored in the position of the HashSet<Integer> structure in the array is stored in that position*/ public void loadforbidden (caminho da sequência) {arquivo proibiddenfile = novo arquivo (caminho); FileInputStream fileInputStream; tente {fileInputStream = new FileInputStream (proibiddenfile); InputStreamReader inputStreamReader = new InputStreamReader (FileInputStream, "UTF-8"); BufferReader buffaredReader = new BufferredReader (InputStreamReader); Strings s; while ((s = bufferredReader.readline ())! = null) {s = s.trim (); if (s.Length ()> 0) {keystring.add (s); int i = s.charat (0); if (keyLength [i] == null) {// mascarar hashset hashset hashset <TEGER> a = novo hashset <Teger> (); A.Add (S.Length ()); keyLength [i] = a; } else {keyLength [i] .add (s.Length ()); }}} fileInputStream.close (); BufferredReader.Close (); fileInputStream.close (); } catch (filenotfoundException e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); }} public static void main (string [] args) {// System.out.println (proibidden.getforbidden (). leia ("AV ATRIZ NIHAO")); int i = 'você'; System.out.println (i); }}O exposto acima é o código -chave para Java implementar a função de palavra de bloqueio. Espero que seja útil para o aprendizado de todos.