22 java常用方法

來源:互聯網
上載者:User

標籤:tabs   array   存在   efi   rip   getname   map   static   .com   

 /**     * 通過正則擷取該目錄下滿足條件的所有目錄     * @param luceneFilePathRegular  正則目錄,如/user/solrindex/Regex     * @return 滿足Regex的目錄集合 list     */    public static List<String> fetchDirByRegularLinux(String luceneFilePathRegular){        List<String> list=new ArrayList<>();        //分割擷取主目錄        int len= luceneFilePathRegular.lastIndexOf(EtlConstants.LINUX_ROUTE_SEGMENT)+1;        String mainDir=luceneFilePathRegular.substring(0, len);        String regular=luceneFilePathRegular.substring(len,luceneFilePathRegular.length());        File dir=new File(mainDir);        if(dir.exists() && dir.isDirectory()){            File [] arr= dir.listFiles();            for (File file : arr) {                if (file.exists() && file.isDirectory()) {                    String fileName = file.getName();                    if (matchStr(fileName, regular)) {                        list.add(file.getAbsolutePath()+SolrUtil.INDEX_DIR_SUFFIX);                    }                }            }        }        if(list.size()>0){            LOGGER.info("通過正則匹配到的Solr目錄有:");            for (String s : list) {                LOGGER.info(s);            }        }else{            LOGGER.error("路徑{}下,不存在滿足正則:{}條件的目錄", dir, regular);        }        return  list;    }

 

    /**     * @Method Description:按正則表示是匹配字串     * @param str     * @param regular     * @return     */    public static Boolean matchStr(String str, String regular) {        Pattern pattern = Pattern.compile(regular);        Matcher matcher = pattern.matcher(str);        return matcher.matches();    }    /**     * base的exponent次方     * @param base     * @param exponent     * @return     */    public static int pow(int base,int exponent){        int result=1;        for(int i=0;i<exponent;i++){            result=result*base;        }        return result;    }

 

public static final Map<String, Long> DATE_MAP = new HashMap<String, Long>() {        {            put("month", 1000 * 60 * 60 * 24L);            put("day", 1000 * 60 * 60 * 24L);            put("hour", 1000 * 60 * 60L);        }    };

 

 

/**     * 把字串轉化成指定位元size     * 不足,前面補充supplementValue     * 超出,isCutFront為true截取前面的size位     * @param original     * @param size     * @param supplementValue     * @return     */    public static String supplementStringFront(String original,int size,String supplementValue){        return cutString(original,size,true,supplementValue,true);    }    /**     * @param original     * @param size     * @param supplementValue     * @param front     * @return     */    /**     * 把字串轉化成指定位元size     * 不足,isFrontSupplement為true,前面補充supplementValue,isFrontSupplement為false,後面補充supplementValue     * 超出,isCutFront為true截取前面的size位,isCutFront為false,截取後面的size位     * @param original     * @param size     * @param isFrontSupplement     * @param supplementValue     * @param isCutFront     * @return     */    public static String cutString(String original,int size,Boolean isFrontSupplement,String supplementValue,Boolean isCutFront){        int length=original.length();        String result = null;        if(length==size){            result=original;        }else if(length<size){            if(isFrontSupplement){                result=supplementValue+original;                while(result.length()<size){                    result=supplementValue+result;                }            }else{                result=original+supplementValue;                while(result.length()<size){                    result=result+supplementValue;                }            }        }        //length>size        else{            if(isCutFront){                result=original.substring(0,size);            }else {                result=original.substring(original.length()-size,original.length());            }        }        return  result;    }

 

22 java常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.