文件目录删除操作类
package com.gg.demo.dir; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 文件目录删除操作类 **/ public class DeleteDirectory { /** * 删除空目录 * * @param dir * 将要删除的目录路径 */ private static void doDeleteEmptyDir(String dir) { boolean success = (new File(dir)).delete(); if (success) { System.out.println("Successfully deleted empty directory: " + dir); } else { System.out.println("Failed to delete empty directory: " + dir); } } /** * 递归删除目录下的所有文件及子目录下所有文件 * * @param dir * 将要删除的文件目录 * @return boolean Returns "true" if all deletions were successful. If a * deletion fails, the method stops attempting to delete and returns * "false". */ private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); // 递归删除目录中的子目录下 for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); } public static void traverseFolder2(String path) { File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (files.length == 0) { System.out.println("文件夹是空的!"); return; } else { for (File file2 : files) { if (file2.isDirectory()) { System.out.println("文件夹:" + file2.getAbsolutePath()); traverseFolder2(file2.getAbsolutePath()); } else { System.out.println("文件:" + file2.getAbsolutePath()); } } } } else { System.out.println("文件不存在!"); } } public static void getDir(String strPath) { File f = new File(strPath); if (f.isDirectory()) { File[] fList = f.listFiles(); for (File f1 : fList) { System.out.println(f1.getName()); } } } //获取N天前的截止日期 public static Date getNextDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -8); date = calendar.getTime(); return date; } //删除 截止日期之前的文件夹 public static void getNextDay22(String strPath,Date date) { Calendar ca = Calendar.getInstance(); // Date curDate; // Date b; // 假设现在你已经实例化了a和b // curDate.after(b)返回一个boolean,如果curDate的时间在b之后(不包括等于)返回true // b.before(curDate)返回一个boolean,如果b的时间在curDate之前(不包括等于)返回true // curDate.equals(b)返回一个boolean,如果a的时间和b相等返回true //获取删除的截止日期 Date curDate = getNextDay(date); File f = new File(strPath); if (f.exists()) { File[] files = f.listFiles(); if (files.length == 0) { System.out.println("文件夹是空的!"); return; } else { for (File file2 : files) { if (file2.isDirectory()) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); try { Date d = sdf.parse(file2.getName() + " " + "00:00:00"); if(d.before(curDate)){ System.out.println(strPath + "/" + file2.getName()); deleteDir(new File(strPath + "/" + file2.getName())); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { System.out.println("文件:" + file2.getAbsolutePath()); } } } } else { System.out.println("文件不存在!"); } } /** * 测试 */ public static void main(String[] args) { // doDeleteEmptyDir("new_dir1"); // String newDir2 = "F:/data/20170525"; // boolean success = deleteDir(new File(newDir2)); // if (success) { // System.out.println("Successfully deleted populated directory: " + newDir2); // } else { // System.out.println("Failed to delete populated directory: " + newDir2); // } // // traverseFolder2("F:/data"); // getDir("F:/data"); Date d = new Date(); getNextDay22("F:/data", d); // Date nextDay = getNextDay(d); // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); // String dateNowStr = sdf.format(nextDay); // System.out.println("格式化后的日期:" + dateNowStr); } }