你会去创建一个线程去处理压缩日志并删除吗?

代码如下

new Thread(() -> {
            int hour = 11;
            boolean flag = false;
            while (true) {
                if (LocalTime.now().getHour() == hour) {
                    if(!flag){
                        flag = true;
                        String source = "C:\\MY_IDEA\\demo_01\\ecology\\log\\integration";
                        FileInputStream fis = null;
                        FileOutputStream fos = null;
                        ZipOutputStream zos = null;
                        String filePath = null;
                        Boolean isZip = false;
                        File catagory = new File(source);
                        File[] files = catagory.listFiles();
                        String date = LocalDate.now().minusDays(1).toString().replace("-", "");
                        //设置压缩的目标文件名称
//                        String target = source + "\\ecology_" + date + ".zip";
                        String target = source + "\\integration_" + date + ".zip";
                        System.out.println(target);
                        //找具体的前一天的ecology日志路径
                        if (catagory.exists()) {
                            for (File path : files) {
                                if (path.getName().endsWith(date + ".log")) {
                                    filePath = path.toString();
                                    System.out.println("ecology日志文件目录是:" + path.toString());
                                    break;
                                }
                            }
                        }
                        if (null != filePath) {
                            //targetFile压缩的log日志全路径
                            File targetFile = new File(filePath);
                            try {
                                //目标zip文件(包括路径写入文件输出流)
                                fos = new FileOutputStream(target);
                                //封装进zip输出流
                                zos = new ZipOutputStream(fos);

                                if (targetFile.exists()) {
                                    try {
                                        //log文件简历输出流
                                        fis = new FileInputStream(targetFile);
                                        ZipEntry ze = new ZipEntry(targetFile.getName());
                                        zos.putNextEntry(ze);
                                        byte[] content = new byte[1024];
                                        int len;
                                        while ((len = fis.read(content)) != -1) {
                                            zos.write(content, 0, len);
                                            zos.flush();
                                        }
                                    } catch (IOException e) {
                                        System.out.println("ecology日志压缩写入zip文件异常!");
                                    } finally {
                                        try {
                                            if (fis != null) {
                                                fis.close();
                                            }
                                        } catch (IOException e) {
                                            System.out.println("ecology日志压缩输入流关闭异常!");
                                        }
                                    }
                                }
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    if (zos != null) {
                                        zos.close();
                                    }
                                } catch (IOException e) {
                                    System.out.println("ecology日志压缩压缩输出流关闭异常!");
                                }
                            }

                            for (File path : files) {
                                if (path.getName().endsWith(date + ".zip")) {
                                    isZip = true;
                                    break;
                                }
                            }
                            System.out.println(isZip);
                            if (isZip) {
//                                if(targetFile.getParentFile().getName() == "ecology"){
//                                    targetFile.delete();
//                                }
                                System.out.println("targetFile是" +targetFile);
                                System.out.println(targetFile.getParentFile().getName());
                                System.out.println(targetFile.getParentFile().getParentFile().getName());
                                System.out.println(targetFile.getParentFile().getParentFile().getParentFile().getName());
                                if("integration".equals(targetFile.getParentFile().getName()) && "log".equals(targetFile.getParentFile().getParentFile().getName()) && "ecology".equals(targetFile.getParentFile().getParentFile().getParentFile().getName())){
                                    targetFile.delete();
                                }else{
//                                    log.info("integration的目录不正确,请检查目录是否正确!");、
                                    System.out.println("integration的目录不正确,请检查目录是否正确!");
                                }
                            }else{
//                                log.info("该文件夹不是ecology\log,无法删除对应日志");
                                System.out.println("该文件夹不是ecology\\log\\integration,无法删除对应日志!");
                            }
                        } else {
                            System.out.println("ecology日志前一天日志已经压缩并删除!");
                        }
                    }

                    try {
                        TimeUnit.SECONDS.sleep(15);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(LocalTime.now().getHour() == 3){
                        flag = false;
                    }
                }
            }
        }).start();

 

你可能感兴趣的:(多线程)