基于common-io自动监控文件及文件变化和同步文件的工具类SynFile
Posted 阿啄debugIT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于common-io自动监控文件及文件变化和同步文件的工具类SynFile相关的知识,希望对你有一定的参考价值。
基于common-io自动监控文件及文件变化和同步文件的工具类
import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SynFile
private static final Logger log;
private static String srcPath;
private static String dstPath;
public static String oldPath;
private static String checkPath;
private static int MAX_FILE_COUNT;
private ConcurrentLinkedQueue<File> pendingQueue = new ConcurrentLinkedQueue();
private ConcurrentLinkedQueue<File> compressQueue = new ConcurrentLinkedQueue();
public static void main(String[] args) throws Exception
SynFile test = new SynFile();
test.watch();
test.syncDetentionFiles();
private void syncDetentionFiles()
log.info("sync Detention Files Start...");
File srcDir = new File(srcPath);
if ((!srcDir.exists()) || (!srcDir.isDirectory()))
log.error("srcPath : is not exist!", srcPath);
else
log.info("srcPath : exist! Scan files ...", srcPath);
File[] srcFileArray = srcDir.listFiles();
assert (srcFileArray != null);
log.info("srcPath : contains files", srcPath, Integer.valueOf(srcFileArray.length));
for (File file : srcFileArray)
String fileName = file.getName();
if (!fileName.contains("_"))
log.error("**********文件: 没有时间戳**********", fileName);
else
int index = fileName.lastIndexOf("_");
String fileDate = fileName.substring(index + 1, index + 1 + 8);
String checkDir = checkPath + fileDate + "/";
if ((!FileUtil.isFileExist(checkDir + fileName)) && (!FileUtil.isFileExist(dstPath + fileName)))
log.info("File: is neither in dstPath , nor in checkDir ", new Object[] fileName, dstPath, checkDir );
FileUtil.copy(file, new File(dstPath + fileName));
log.info("File: copy to ", fileName, dstPath + fileName);
this.compressQueue.add(file);
private void watch() throws Exception
FileFilter filter = FileFilterUtils.and(new IOFileFilter[] new MyFileFilter() );
FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(srcPath, filter);
fileAlterationObserver.addListener(new FileAlterationListenerAdaptor()
public void onDirectoryCreate(File directory)
if (SynFile.log.isInfoEnabled())
SynFile.log.info("onDirectoryCreate");
super.onDirectoryCreate(directory);
public void onDirectoryDelete(File directory)
if (SynFile.log.isInfoEnabled())
SynFile.log.info("onDirectoryDelete");
super.onDirectoryDelete(directory);
public void onFileChange(File file)
if (SynFile.log.isInfoEnabled())
SynFile.log.info("onFileChange");
super.onFileChange(file);
public void onFileCreate(File file)
SynFile.log.info("onFileCreate ", file.getAbsoluteFile());
SynFile.this.pendingQueue.add(file);
super.onFileCreate(file);
public void onFileDelete(File file)
if (SynFile.log.isInfoEnabled())
SynFile.log.info("onFileDelete");
super.onFileDelete(file);
public void onStart(FileAlterationObserver observer)
SynFile.log.info("onStart");
super.onStart(observer);
while (!SynFile.this.pendingQueue.isEmpty())
File item = (File)SynFile.this.pendingQueue.poll();
FileUtil.copy(item, new File(SynFile.dstPath + item.getName()));
SynFile.this.compressQueue.add(item);
if (SynFile.this.compressQueue.size() > SynFile.MAX_FILE_COUNT)
FileUtil.compress(SynFile.this.compressQueue, SynFile.oldPath + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".zip");
);
FileAlterationMonitor filealterationMonitor = new FileAlterationMonitor(1000L);
filealterationMonitor.addObserver(fileAlterationObserver);
filealterationMonitor.start();
static
log = LoggerFactory.getLogger(SynFile.class);
srcPath = "/home/tmpuser/gdcymod2/";
dstPath = "/home/tmpuser/gdcymot2/work/";
oldPath = "/home/tmpuser/gdcymot2/old/";
checkPath = "/home/tmpuser/gdcymot2/bak/";
MAX_FILE_COUNT = 20000;
以上是关于基于common-io自动监控文件及文件变化和同步文件的工具类SynFile的主要内容,如果未能解决你的问题,请参考以下文章