1 package org.zln.module1.demo1; 2 3 import org.apache.log4j.Logger; 4 5 import java.io.File; 6 7 /** 8 * 删除指定根目录下,及其子目录中,指定后缀的文件 9 * Created by coolkid on 2015/6/13 0013. 10 */ 11 12 public class DeleteSuffix { 13 /*默认待删除文件后缀*/ 14 private static final String SUFFIX = ".jar"; 15 16 protected static Logger logger = Logger.getLogger(DeleteSuffix.class); 17 18 public static void main(String[] args) { 19 /*预处理*/ 20 if (null == args || args.length == 0||args.length>2){ 21 throw new RuntimeException("命令参数不正确,格式为 java 根路径 后缀"); 22 } 23 24 /*根路径*/ 25 File rootPathFile = new File(args[0]); 26 if (!rootPathFile.exists()){ 27 throw new RuntimeException("路径错误,不存在:"+rootPathFile.getAbsolutePath()); 28 } 29 /*待删除后缀*/ 30 String suffixCurr = args.length==2?args[1]:SUFFIX; 31 32 deleteSuffixFile(rootPathFile,suffixCurr); 33 34 35 } 36 37 /** 38 * 删除指定后缀的文件 39 * @param rootPathFile 根路径 40 * @param suffixCurr 后缀 41 */ 42 private static void deleteSuffixFile(File rootPathFile, String suffixCurr) { 43 if (rootPathFile.isDirectory()){ 44 File[] files = rootPathFile.listFiles(); 45 for (File tempFile:files){ 46 deleteSuffixFile(tempFile,suffixCurr); 47 } 48 }else { 49 String suffix = rootPathFile.getName().substring(rootPathFile.getName().lastIndexOf(".")); 50 if (suffixCurr.equalsIgnoreCase(suffix)){ 51 logger.debug("删除:"+rootPathFile.getAbsolutePath()); 52 rootPathFile.delete(); 53 } 54 } 55 } 56 }
时间: 2025-01-10 16:45:56