思路:在每个空文件夹下创建空文件,同步后再删除
package org.zln.module1.demo1; import org.apache.log4j.Logger; import java.io.File;
import java.io.IOException; /**
* Created by coolkid on 2015/6/12 0012.
*/
public class CreateFileInEmptyDir {
protected static Logger logger = Logger.getLogger(CreateFileInEmptyDir.class); private static final String createFlag = "create";
private static final String fillFileName = "填充文件.txt"; public static void main(String[] args) throws IOException {
String rootDir;
String delFlag;
if (null == args||args.length != 2){
throw new RuntimeException("输入参数个数不正确");
}else {
rootDir = args[0];
delFlag = args[1];
}
logger.debug("对目录:"+rootDir+" 进行"+(createFlag.equals(delFlag)?"创建":"删除"));
File rootFile = new File(rootDir);
if (createFlag.equals(delFlag)){//默认创建
createFileInEmptyDir(rootFile);
}else {//删除
deleteFillFile(rootFile);
}
} /**
* 在跟路径下,遍历所有的目录,如果该目录为空,则创建一个文件
* @param rootFile 根目录文件
*/
private static void createFileInEmptyDir(File rootFile) throws IOException {
if (rootFile.isDirectory()){
File[] listFiles = rootFile.listFiles();
if (0 == listFiles.length){
createTxtFile(rootFile,fillFileName);
logger.debug(rootFile.getAbsolutePath()+"\t为空目录,创建填充文件"); }else {
for (int i = 0; i < listFiles.length; i++) {
createFileInEmptyDir(listFiles[i]);
}
}
}
} private static void createTxtFile(File rootFile,String fileName) throws IOException {
File newFile = new File(rootFile,fileName);
newFile.createNewFile();
} /**
* 删除指定目录下的填充文件
* @param root
*/
private static void deleteFillFile(File root){
if (root.isDirectory()){//是目录
File[] files = root.listFiles();
for (File tempFile:files){
deleteFillFile(tempFile);
}
}else {//是文件
String fileName = root.getName();
if (fillFileName.equals(fileName)){
logger.debug("删除:"+root.getAbsolutePath());
root.delete();
}
}
}
}
依赖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
优化:创建与删除可以同时进行
添加一个方法
/**
* 创建或删除空文件在指定路径及其子路径下
* @param rootFile 根路径
*/
private static void createOrDeleteFile(File rootFile) throws IOException {
if (rootFile.isDirectory()){
File[] listFiles = rootFile.listFiles();
if (0 == listFiles.length){
createTxtFile(rootFile,fillFileName);
logger.debug(rootFile.getAbsolutePath()+"\t为空目录,创建填充文件");
}else if (1 < listFiles.length){
for (File temp:listFiles){
if (temp.isDirectory()){
createOrDeleteFile(temp);
}else {
if (fillFileName.equals(temp.getName())){
logger.debug("填充文件失效,删除:"+temp.getAbsolutePath());
temp.delete();
}
}
}
}
}
}