思路:在每个空文件夹下创建空文件,同步后再删除
1 package org.zln.module1.demo1; 2 3 import org.apache.log4j.Logger; 4 5 import java.io.File; 6 import java.io.IOException; 7 8 /** 9 * Created by coolkid on 2015/6/12 0012. 10 */ 11 public class CreateFileInEmptyDir { 12 protected static Logger logger = Logger.getLogger(CreateFileInEmptyDir.class); 13 14 private static final String createFlag = "create"; 15 private static final String fillFileName = "填充文件.txt"; 16 17 public static void main(String[] args) throws IOException { 18 String rootDir; 19 String delFlag; 20 if (null == args||args.length != 2){ 21 throw new RuntimeException("输入参数个数不正确"); 22 }else { 23 rootDir = args[0]; 24 delFlag = args[1]; 25 } 26 logger.debug("对目录:"+rootDir+" 进行"+(createFlag.equals(delFlag)?"创建":"删除")); 27 File rootFile = new File(rootDir); 28 if (createFlag.equals(delFlag)){//默认创建 29 createFileInEmptyDir(rootFile); 30 }else {//删除 31 deleteFillFile(rootFile); 32 } 33 } 34 35 /** 36 * 在跟路径下,遍历所有的目录,如果该目录为空,则创建一个文件 37 * @param rootFile 根目录文件 38 */ 39 private static void createFileInEmptyDir(File rootFile) throws IOException { 40 if (rootFile.isDirectory()){ 41 File[] listFiles = rootFile.listFiles(); 42 if (0 == listFiles.length){ 43 createTxtFile(rootFile,fillFileName); 44 logger.debug(rootFile.getAbsolutePath()+" 为空目录,创建填充文件"); 45 46 }else { 47 for (int i = 0; i < listFiles.length; i++) { 48 createFileInEmptyDir(listFiles[i]); 49 } 50 } 51 } 52 } 53 54 private static void createTxtFile(File rootFile,String fileName) throws IOException { 55 File newFile = new File(rootFile,fileName); 56 newFile.createNewFile(); 57 } 58 59 /** 60 * 删除指定目录下的填充文件 61 * @param root 62 */ 63 private static void deleteFillFile(File root){ 64 if (root.isDirectory()){//是目录 65 File[] files = root.listFiles(); 66 for (File tempFile:files){ 67 deleteFillFile(tempFile); 68 } 69 }else {//是文件 70 String fileName = root.getName(); 71 if (fillFileName.equals(fileName)){ 72 logger.debug("删除:"+root.getAbsolutePath()); 73 root.delete(); 74 } 75 } 76 } 77 }
依赖jar包
执行脚本:
#进入执行目录 - 程序根目录 cd E:GitHub oolsJavaEEDevelopoutproductionLesson1_JavaSe_Demo1 #设置class路径 set CLASSSERVER_HOME=E:GitHub oolsJavaEEDevelopGlobalLib set CLASSPATH=. set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%log4jlog4j-1.2.17.jar set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%log4jlog4j-api-2.2.jar set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%log4jlog4j-core-2.2.jar #根据需求执行指令 #对指定目录及其子目录的空文件夹创建空文件 java org.zln.module1.demo1.CreateFileInEmptyDir E:GitHub oolsJavaEEDevelop create #删除创建的空文件 java org.zln.module1.demo1.CreateFileInEmptyDir E:GitHub oolsJavaEEDevelop delete
日志配置:
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order): # # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=DEBUG, stdout,FILE log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n #输出到文件 log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.Append=true log4j.appender.FILE.File=D:\log\Lesson1_JavaSe_Demo1.log log4j.appender.FILE.Threshold=DEBUG log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n log4j.appender.FILE.MaxFileSize=10MB # Print only messages of level ERROR or above in the package noModule. log4j.logger.noModule=FATAL # OpenSymphony Stuff log4j.logger.freemarker=INFO log4j.logger.com.opensymphony=INFO log4j.logger.com.opensymphony.xwork2.ognl=ERROR log4j.logger.org.apache.struts2=WARN log4j.logger.org.apache.struts2.components=WARN log4j.logger.org.apache.struts2.dispatcher=WARN log4j.logger.org.apache.struts2.convention=INFO # Spring Stuff log4j.logger.org.springframework=WARN
优化:创建与删除可以同时进行
添加一个方法
1 /** 2 * 创建或删除空文件在指定路径及其子路径下 3 * @param rootFile 根路径 4 */ 5 private static void createOrDeleteFile(File rootFile) throws IOException { 6 if (rootFile.isDirectory()){ 7 File[] listFiles = rootFile.listFiles(); 8 if (0 == listFiles.length){ 9 createTxtFile(rootFile,fillFileName); 10 logger.debug(rootFile.getAbsolutePath()+" 为空目录,创建填充文件"); 11 }else if (1 < listFiles.length){ 12 for (File temp:listFiles){ 13 if (temp.isDirectory()){ 14 createOrDeleteFile(temp); 15 }else { 16 if (fillFileName.equals(temp.getName())){ 17 logger.debug("填充文件失效,删除:"+temp.getAbsolutePath()); 18 temp.delete(); 19 } 20 } 21 } 22 } 23 } 24 }