标签:
思路:在每个空文件夹下创建空文件,同步后再删除
1 protected static Logger logger = Logger.getLogger(CreateFileInEmptyDir.class); 2 3 private static final String createFlag = "create"; 4 private static final String fillFileName = "填充文件.txt"; 5 6 public static void main(String[] args) throws IOException { 7 String rootDir; 8 String delFlag; 9 if (null == args||args.length != 2){ 10 throw new RuntimeException("输入参数个数不正确"); 11 }else { 12 rootDir = args[0]; 13 delFlag = args[1]; 14 } 15 logger.debug("对目录:"+rootDir+" 进行"+(createFlag.equals(delFlag)?"创建":"删除")); 16 File rootFile = new File(rootDir); 17 if (createFlag.equals(delFlag)){//默认创建 18 createFileInEmptyDir(rootFile); 19 }else {//删除 20 deleteFillFile(rootFile); 21 } 22 } 23 24 /** 25 * 在跟路径下,遍历所有的目录,如果该目录为空,则创建一个文件 26 * @param rootFile 根目录文件 27 */ 28 private static void createFileInEmptyDir(File rootFile) throws IOException { 29 if (rootFile.isDirectory()){ 30 File[] listFiles = rootFile.listFiles(); 31 if (0 == listFiles.length){ 32 createTxtFile(rootFile,fillFileName); 33 logger.debug(rootFile.getAbsolutePath()+"\t为空目录,创建填充文件"); 34 35 }else { 36 for (int i = 0; i < listFiles.length; i++) { 37 createFileInEmptyDir(listFiles[i]); 38 } 39 } 40 } 41 } 42 43 private static void createTxtFile(File rootFile,String fileName) throws IOException { 44 File newFile = new File(rootFile,fileName); 45 newFile.createNewFile(); 46 } 47 48 /** 49 * 删除指定目录下的填充文件 50 * @param root 51 */ 52 private static void deleteFillFile(File root){ 53 if (root.isDirectory()){//是目录 54 File[] files = root.listFiles(); 55 for (File tempFile:files){ 56 deleteFillFile(tempFile); 57 } 58 }else {//是文件 59 String fileName = root.getName(); 60 if (fillFileName.equals(fileName)){ 61 logger.debug("删除:"+root.getAbsolutePath()); 62 root.delete(); 63 } 64 } 65 }
依赖jar包
执行脚本:
#进入执行目录 - 程序根目录 cd E:\GitHub\tools\JavaEEDevelop\out\production\Lesson1_JavaSe_Demo1 #设置class路径 set CLASSSERVER_HOME=E:\GitHub\tools\JavaEEDevelop\GlobalLib set CLASSPATH=. set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%\log4j\log4j-1.2.17.jar set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%\log4j\log4j-api-2.2.jar set CLASSPATH=%CLASSPATH%;%CLASSSERVER_HOME%\log4j\log4j-core-2.2.jar #根据需求执行指令 #对指定目录及其子目录的空文件夹创建空文件 java org.zln.module1.demo1.CreateFileInEmptyDir E:\GitHub\tools\JavaEEDevelop create #删除创建的空文件 java org.zln.module1.demo1.CreateFileInEmptyDir E:\GitHub\tools\JavaEEDevelop 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
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4572625.html