Delete Files with same Prefix String using Java

前端 未结 11 1730
梦谈多话
梦谈多话 2020-12-16 09:11

I have around 500 text files inside a directory with a same prefix in their filename say dailyReport_.

The latter part of the file is the date of the fi

11条回答
  •  不思量自难忘°
    2020-12-16 09:51

    Use FileFilter like so:

    File dir = new File();
    File[] toBeDeleted = dir.listFiles(new FileFilter() {
      boolean accept(File pathname) {
         return (pathname.getName().startsWith("dailyReport_08") && pathname.getName().endsWith(".txt"));
      } 
    
    for (File f : toBeDeleted) {
       f.delete();
    }
    

提交回复
热议问题