• 常用工具类


    常用工具类

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.math.BigDecimal;
    import java.util.Collection;
    import java.util.Map;
    import java.util.Random;
    
    import com.google.common.base.Strings;
    import com.nonobank.common.base.RandomNumbers;
    
    /**
     * 常用工具类
     * @author changbingbing
     *
     */
    public class Utils {
        final static int BUFFER_SIZE = 4096;
        private final static String[] RANDOM_CODE = { "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "a", "b", "c", "d", "", "e", "f", "g", "h",
                "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
                "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H",
                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
                "V", "W", "X", "Y", "Z" };

     生成8位随机整数

    /**
         * 
         * @Title: get8Random
         * @Description:生成8位随机整数
         * @return
         * @Return: String
         *
         */
        private static String get8Random() {
            String arr[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            String myRandom = "";
            for (int i = 0; i < 8; i++) {
                int lotteryNumber = new Random().nextInt(10);
                myRandom = myRandom + arr[lotteryNumber];
            }
            return myRandom;
        }

     屏蔽身份证

    /**
         * 屏蔽身份证
         * 
         * @param idnum
         * @return
         */
        public static String hideIdnum(String idnum) {
            if (Strings.isNullOrEmpty(idnum))
                return "";
            if (idnum.length() == 18)
                return idnum.substring(0, 6) + "********" + idnum.substring(14);
            else if (idnum.length() == 15)
                return idnum.substring(0, 6) + "********" + idnum.substring(12);
            return "";
        }

     将生成的html协议文件输出到指定的文件

    /**
         * 将生成的html协议文件输出到指定的文件
         * 
         * @param content
         * @param path
         * @param fileName
         * @throws IOException
         */
        public static void writeToFile(String content, String path, String fileName)
                throws IOException {
            String head = "<html>"
                    + "<head>"
                    + "<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />"
                    + "</head>";
            String tail = "</body></html>";
            try (FileWriter fw = new FileWriter(path + File.separator + fileName)) {
                fw.write(head);
                fw.write(content);
                fw.write(tail);
            }
        }

     判断对象或对象数组中每一个对象是否为空 对象为null,字符序列长度为0,集合类、Map为empty

    /**
         * 
         * @Title: isNull
         * @Description:判断对象或对象数组中每一个对象是否为� 对象为null,字符序列长度为0,集合类、Map为empty
         * @param obj
         * @return
         * @throws IOException
         * @Return: boolean
         *
         */
        public static boolean isNullOrEmpty(Object obj) {
            if (obj == null)
                return true;
    
            if (obj instanceof CharSequence)
                return ((CharSequence) obj).length() == 0;
    
            if (obj instanceof Collection)
                return ((Collection) obj).isEmpty();
    
            if (obj instanceof Map)
                return ((Map) obj).isEmpty();
    
            if (obj instanceof Object[]) {
                Object[] object = (Object[]) obj;
                if (object.length == 0) {
                    return true;
                }
                boolean empty = true;
                for (int i = 0; i < object.length; i++) {
                    if (!isNullOrEmpty(object[i])) {
                        empty = false;
                        break;
                    }
                }
                return empty;
            }
            return false;
        }

     2个double相除,并传入精度,得出结论

    /**
         * 
         * @Title: div
         * @Description:2个double相除,并传入精度,得出结论
         * @param d1
         * @param d2
         * @param len
         * @return
         * @Return: double
         *
         */
        public static double div(double d1, double d2, int len) {// 进行除法运算
            BigDecimal b1 = new BigDecimal(d1);
            BigDecimal b2 = new BigDecimal(d2);
            return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
        }

     保留2位有效数字,四舍五入

    /**
         * 保留2位有效数字,四舍五入
         * @param amount
         * @return
         */
        public static BigDecimal Scale(BigDecimal amount){
            return amount.setScale(2, BigDecimal.ROUND_HALF_UP);
        }

     将InputStream转换成某种字符编码的String

    /**
         * 将InputStream转换成某种字符编码的String
         * 
         * @param in
         * @param encoding
         * @return
         * @throws Exception
         */
        public static String InputStreamTOString(InputStream in, String encoding)
                throws Exception {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] data = new byte[BUFFER_SIZE];
            int count = -1;
            while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
                outStream.write(data, 0, count);
    
            data = null;
            return new String(outStream.toByteArray(), encoding);
        }

     生成N位随机数

    /**
         * 生成N位随机数�
         * 
         * @param length
         *            随机长度
         * @return
         */
        public static String generateRandomNum(int length) {
            Integer verifyNumInt = RandomNumbers.randomInt(length);
            return verifyNumInt.toString();
        }
  • 相关阅读:
    1.初识Redis
    2.API的理解和使用
    8.rabbitmq RPC模拟微服务架构中的服务调用
    9.[完]其他常用的rabbitmq的参数和设置
    6.Header交换机之模拟验证用户身份
    C#中复制文件夹及文件的两种方法
    python通过递归将多维字典转化为二维
    python venv flask gunicorn 部署与 pycharm 连接
    Nagios(centos 6.5)调用NSClient++/NRPE+Powershell脚本(windows server 2008 r2)监控网络情况
    Nagios 调用华为云短信平台进行报警
  • 原文地址:https://www.cnblogs.com/cb0327/p/5505804.html
Copyright © 2020-2023  润新知