Ecology-定时清除log文件代码

2023-12-22 11:22:03
package com.kaka.workflow.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.schedule.BaseCronJob;

import java.io.File;
import java.io.FileWriter;

/***
* @MethodName: 删除日志文件
* @Description:
* @Param:
* @return:
* @Author: kaka
* @Date: 2023/8/23 10:13
*/
public class DeleteLogFile extends BaseCronJob {
    private static Log log= LogFactory.getLog(DeleteLogFile.class);
    @Override
    public void execute()  {
        try{
            removeFile(new File("D:\\WEAVER\\ecology\\log")); //删除ecology日志
            removeFile(new File("D:\\WEAVER\\Resin\\logs")); //删除resin日志
            removeFile(new File("D:\\WEAVER\\Resin\\log")); //删除resin日志
        }catch (Exception e){
        log.info(e.getMessage());
        }
    }

    /***
    * @MethodName: removeFile
    * @Description:  遍历文件夹下的文件并打印出来
    * @Param: [file]
    * @return: void
    * @Author: kaka
    * @Date: 2023/8/23 10:56
    */
        public static void removeFile(File file) throws  Exception{
            System.out.println(file.getName());
            File[] files = file.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isFile()) {
                        files[i].setReadable(true); //读权限
                        files[i].setWritable(true);//写权限
                        files[i].setExecutable(true);//更改
                        FileWriter fileWriter = new FileWriter(files[i]);
                        fileWriter.write("");
                        fileWriter.close();
                        files[i].delete(); //删除
                    } else if (files[i].isDirectory()) {
                        removeFile(new File(files[i].getAbsolutePath()));
                    }
                }

            }
        }

}

运行结果:

文章来源:https://blog.csdn.net/javakaka666/article/details/135145230
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。