• support@answerspoint.com

How to delete files in java when find according to older then N days and file type

1735

how to delete files in java when we want to use old, and file type in an folder

please give the complete code

  • Java

  • asked 6 years ago
  • Sunny Solu

1Answer


0
public void remove_old_files(String fiile_path) {
        try {
            File directory = new File(fiile_path);
            if (directory.exists()) {
                File[] listFiles = directory.listFiles();
                Date today = new Date();
                long purgeTime1 = today.getTime();
                long backtime = new Long("2592000000");//2592000000
                long purgeTime = (purgeTime1 - backtime);
                for (File listFile : listFiles) {
                    if (listFile.lastModified() < purgeTime) {
                        if (listFile.getName().endsWith(".dat")) {
                            if (!listFile.delete()) {
                                System.err.println("Unable to delete file: " + listFile);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
        }
    }

Call to function

remove_old_files("d:/datafolder");

  • answered 6 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 6 years ago
  • viewed 1735 times
  • active 6 years ago

Best Rated Questions