public class DiskPurgeTasklet implements Tasklet { private static final Logger LOGGER = LogManager.getLogger(DiskPurgeTasklet.class); private static final String FILE_LIST_PATH = "FileList.txt"; private static final String FILE_PURGE_PATH = "file-purge.xml"; @Override public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { LOGGER.info("DiskPurgeTasklet start ..."); //1.get file path list SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); long currentTimeMillis = System.currentTimeMillis(); long liveDays = 5*24*60*60*60; File file = new File(FILE_LIST_PATH); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); List<String> pathList = new ArrayList<>(); String path; while ((path = br.readLine()) != null){ pathList.add(path); } LOGGER.info("Total File Sum:" +pathList.size()); List<String> delList = new ArrayList<>(); pathList.stream().forEach(df->fileDelete(df, sdf, currentTimeMillis, liveDays,delList)); LOGGER.info("Deleted File Sum:" +delList.size()); sendEmail(delList,sdf); LOGGER.info("DiskPurgeTasklet end ..."); return RepeatStatus.FINISHED; } private void fileDelete(String pathStr,SimpleDateFormat sdf,long currentTimeMillis,long liveDays, List<String> delList){ File checkFile = new File(pathStr); if(checkFile.exists()){ String[] strArray = pathStr.split("[\\D*\\|\\W*]"); for(String str:strArray){ if(!str.trim().isEmpty() && str.trim().length()==8 && str.trim().startsWith("20")){ long l1 = 0; try { l1 = currentTimeMillis - sdf.parse(str).getTime(); } catch (ParseException e) { LOGGER.info("time parse error; filePath:"+pathStr,e); continue; } if(true){//l1>liveDays long lastModifiedTime = checkFile.lastModified(); long l2 = currentTimeMillis - lastModifiedTime; if(true){//l2>liveDays //checkFile.delete(); LOGGER.info(pathStr + " was deleted"); delList.add(pathStr); break; }else{ continue; } }else{ continue; } } } } } }