浏览代码

修改代码 修复删除空跟目录的bug

fuyu 6 年之前
父节点
当前提交
51b8eb82ff
共有 1 个文件被更改,包括 38 次插入6 次删除
  1. 38 6
      src/main/java/com/zskk/dicom/monitor/utils/MonitorFileUtils.java

+ 38 - 6
src/main/java/com/zskk/dicom/monitor/utils/MonitorFileUtils.java

@@ -104,7 +104,38 @@ public class MonitorFileUtils {
 			return;
 		}
 
-		String[] childFilePaths = dir.list();
+		File[] childFiles = dir.listFiles();
+		if(childFiles == null || childFiles.length == 0) {
+			Configs.sysLog.warn("this dirFilePath is a empty dir \tdirFilePath:" + dirFilePath);
+			return;
+		}
+
+		for (File childFile : childFiles) {
+			if(childFile != null && childFile.isDirectory()) {
+				removeBeforeTodayEmptyDirImpl(childFile.getAbsolutePath());
+			}
+		}
+
+	}
+
+
+	/**
+	 * 移除今天之前创建的空目录
+	 * @param dirFilePath 文件夹目录
+	 */
+	public static void removeBeforeTodayEmptyDirImpl(String dirFilePath) {
+		// 文件的上级目录
+		if(TextUtils.isEmpty(dirFilePath)) {
+			Configs.sysLog.warn("the dirFilePath is null");
+			return;
+		}
+		File dir = new File(dirFilePath);
+		if(!dir.exists() || !dir.isDirectory()) {
+			Configs.sysLog.warn("this dirFilePath not exists or not dir \tdirFilePath:" + dirFilePath);
+			return;
+		}
+
+		File[] childFiles = dir.listFiles();
 		DateFormat df = new SimpleDateFormat("yyyyMMdd");
 		Calendar ca = Calendar.getInstance();
 		ca.setTimeInMillis(dir.lastModified());
@@ -116,23 +147,24 @@ public class MonitorFileUtils {
 			return;
 		}
 
-		if(null == childFilePaths || childFilePaths.length == 0) {
+		if(null == childFiles || childFiles.length == 0) {
 			// 不是同一天
 			if (dir.delete()) {
 				Configs.sysLog.info("Dir is delete suc:" + dirFilePath);
 			} else {
 				Configs.sysLog.info("Dir is delete fail:" + dirFilePath);
 			}
+			return;
 		}
 
-		for (String childFilePath : childFilePaths) {
-			removeBeforeTodayEmptyDir(childFilePath);
+		for (File childFile : childFiles) {
+			if(childFile != null && childFile.isDirectory()) {
+				removeBeforeTodayEmptyDirImpl(childFile.getAbsolutePath());
+			}
 		}
 
 	}
 
-
-
 	/**
 	 * 检测备份目录并返回 备份目标 文件(路径)
 	 *