File 文件一行行读取

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;
          }
        }
      }
    }
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值