• java常用工具方法


    1、两个数相除,保留两位小数

    public static void main(String[] args) {
            double divide = divide(1, 3, 2);
            System.out.println(divide);//0.33
            System.out.println(divide * 100 + "%");//33.0%
        }
        public static double divide(double a, double b, int scale) {
            BigDecimal bd1 = new BigDecimal(Double.toString(a));
            BigDecimal bd2 = new BigDecimal(Double.toString(b));
            return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、文件转字节数组,字节数组转文件

    应用场景: 需要像第三方系统传数据,并且以json格式传。记得中间要用base64把字节转成一个编码串。

        public static void main(String[] args) {
            byte[] bytesByFile = getBytesByFile("D:\\aa.txt");
            //对字节数组进行base64编码
            String encoded = Base64.getEncoder().encodeToString(bytesByFile);
            System.out.println(encoded);
    
            //把base64编码转成字节
            byte[] decoded = Base64.getDecoder().decode(encoded);
            getFileByBytes(decoded,"D:\\","bb.txt");
        }
    
        /**
         * 将文件转换成Byte数组
         * @param pathStr
         * @return
         */
        public static byte[] getBytesByFile(String pathStr) {
            File file = new File(pathStr);
            try {
                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
                byte[] b = new byte[1000];
                int n;
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                fis.close();
                byte[] data = bos.toByteArray();
                bos.close();
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 将Byte数组转换成文件
         * @param bytes
         * @param filePath
         * @param fileName
         */
        public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            File file = null;
            try {
                File dir = new File(filePath);
                if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
                    dir.mkdirs();
                }
                file = new File(filePath + "\\" + fileName);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    layaBox---TypeScript---接口
    【C#项目实战】控制台游戏勇士斗恶龙(1)——游戏初始设置以及开始界面
    高能分享:软件测试十大必问面试题(附带答案)
    TBarCode SDK 11.15.1 Crack
    【CSDN话题挑战赛】【算法题解】颠倒二进制位
    机试:偶数分解
    Vue3 监听属性
    MySQL奇偶数判断
    使用 OpenTracing 和 LightStep 监控无服务器功能
    Spring Session 的原理
  • 原文地址:https://blog.csdn.net/weixin_41919486/article/details/128208418