违禁词校验器

  /*

    文件里存一些违禁词,每行一个

    建一个违禁词校验器,对进来的String数据进行校验(全字符匹配), 返回是否名字违禁词

    要注意文件读取的错误处理

    */

    static  String  filePath ="";

    

    static  String  strLib ;

    

    /**

    * 读取违禁词库

    */

    private  String readFile(){

       

           File f=new File(filePath  );  

           BufferedReader br = null;

           String temp=null;

           StringBuffer sb=new StringBuffer();

           try {

               br=new BufferedReader(new FileReader(f));

               while((temp=br.readLine())!=null){

                   sb.append(temp);

               }

           } catch (IOException e) {

               e.printStackTrace();

           }finally{

               try {

                   br.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }

           return sb.toString();

    } 

    

    


    /**

    * 匹配 

    * @return 

    * @return 

    */

    public  boolean checkString (String str){

        boolean flag=false;

        int  length = str.length()-1;

        char [] stringArr = str.toCharArray(); 

        if(strLib ==null){

            strLib= readFile();

        } 

        for(int i=0 ;i<length;i++){

            if(strLib.indexOf(stringArr[i])!=-1){

                flag = true;

            }

        }

        return flag;

   } 


你可能感兴趣的:(违禁词校验器)