此时情绪此时天,无事小神仙
好好生活,平平淡淡每一天

编辑

CustomUtils

深拷贝

import org.apache.commons.beanutils.PropertyUtils;
PropertyUtils.copyProperties(new Object(),old Object())

获取路径

// 获取项目路径{例如:nice}
String projectPath = getRequest().getContextPath();
// 获取方法名路径{例如:action/searchMethod}
String methodPath = getRequest().getServletPath();
// 获取服务器的绝对路径{例如:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\nice\}
String realPath = getRequest().getRealPath("");

List去重

     /**
     * @Title:removeDuplicate
     * @author:踏步
     * @date:2019年5月23日 下午2:31:40
     * @Description: List去重
     * @param 需要去除重复项的list
     * @return 返回去重后的list
     */
    public static List removeDuplicate(List list) {
        try {
            HashSet hs = new HashSet(list);
            list.clear();
            list.addAll(hs);
        } catch (Exception e) {
            logger.error("去除List重复项removeDuplicate的方法 err", e);
            e.printStackTrace();
        }
        return list;
    }

对象、二进制互转

     /**
     * @Title:objectTurnToBinary
     * @author:踏步
     * @date:2019年7月6日 下午4:12:08
     * @Description: 对象转换成二进制数据
     * @param obj待转换的对象
     * @return byte[] 返回二进制数组
     */
    public static byte[] objectTurnToBinary(Object obj) {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            // 读取对象并转换成二进制数据
            oos.writeObject(obj);
            return bos.toByteArray();
        } catch (IOException e) {
            logger.error("对象转换成二级制数据失败  err", e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * @Title:binaryTurnToObject
     * @author:踏步
     * @date:2019年7月6日 下午4:11:17
     * @Description: 二进制数据转换成对象
     * @param b二进制数组
     * @return Object
     */
    public static Object binaryTurnToObject(byte[] b) {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            // 读取二进制数据并转换成对象
            bis = new ByteArrayInputStream(b);
            ois = new ObjectInputStream(bis);
            return ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            logger.error("二进制数据转换成对象失败  err", e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

字符串相关

     /**
     * @Title:getStringByString
     * @author:踏步
     * @date:2019年7月23日 下午4:36:03
     * @Description: 去除字符串中是所有数字
     * @param str字符串
     * @return String
     */
    public static String getStringByString(String str) throws Exception {
        String reg1 = "[\\d]";
        Pattern p = Pattern.compile(reg1);
        Matcher matcher = p.matcher(str);
        str = matcher.replaceAll("");
        return str;
    }

    /**
     * @Title:getIntByString
     * @author:踏步
     * @date:2019年7月23日 下午4:36:24
     * @Description: 提取字符串中的所有数字
     * @param str字符串
     * @return String
     */
    public static String getIntByString(String str) throws Exception {
        String reg2 = "[^\\d]";
        Pattern p = Pattern.compile(reg2);
        str = p.matcher(str).replaceAll("");
        return str;
    }

    /**
     * @Title:sortString
     * @date:2020年5月14日 下午4:48:16
     * @Description: 对字符串经进行排序
     * @param string 待排序字符串
     * @return String
     * @throws Exception
     */
    public static String sortString(String string) throws Exception{
        QwyUtil.printMethodLogger(logger, "0");
        char[] charArray = string.toCharArray();
        Arrays.sort(charArray);
        return String.valueOf(charArray);
    }
posted @ 2019-07-25 10:28  踏步  阅读(362)  评论(0编辑  收藏  举报