title: Java Delete files older than N days
body: <p>I would like some Java code to Delete files older than N days. </p>
  
  <p>Here is my attempt, but it doesn't work quite right.</p>
  
  <pre><code>public void deleteFilesOlderThanNdays(final int daysBack, final String dirWay) {
  
      System.out.println(dirWay);
      System.out.println(daysBack);
  
      final File directory = new File(dirWay);
      if(directory.exists()){
          System.out.println(" Directory Exists");
          final File[] listFiles = directory.listFiles();          
          final long purgeTime = 
              System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);
  
          System.out.println("System.currentTimeMillis " + 
              System.currentTimeMillis());
  
          System.out.println("purgeTime " + purgeTime);
  
          for(File listFile : listFiles) {
              System.out.println("Length : "+ listFiles.length);
              System.out.println("listFile.getName() : " +listFile.getName());
              System.out.println("listFile.lastModified() :"+
                  listFile.lastModified());
  
              if(listFile.lastModified() &lt; purgeTime) {
                  System.out.println("Inside File Delete");
              }
          }
      } 
      else 
      {
      }
  }
  </code></pre>
  
  <p>Is there some simple code to delete files older than N days in a directory?</p>