标签:
原文地址:http://blog.csdn.net/three_man/article/details/31012903?utm_source=tuicool
介绍
本文主要介绍一种轻量级的文件监控方式,对文件的Create & Change & Delete,都有一个对应的Event方法来进行处理。对于核心的File Monitor Class,我们继承一个监听适配器类FileAlterationListenerAdaptor,这样会更方便,就不用再去重写每个Event方法。
1 package shuai.study.file.generator; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Paths; 6 import java.util.Date; 7 8 import org.apache.log4j.Logger; 9 10 import shuai.study.file.tooler.CommonTooler; 11 12 /** 13 * @author shengshu 14 * 15 */ 16 public class FileGenerator implements Runnable { 17 private static final Logger LOG = Logger.getLogger(FileGenerator.class); 18 19 private String directory = null; 20 21 private static Date date = new Date(); 22 23 public FileGenerator(String directory) { 24 this.directory = directory; 25 } 26 27 private void generator(String directory) { 28 for (int i = 0; i < 10; i++) { 29 String fileName = CommonTooler.getDateTimeFormat(date) + "-" + Thread.currentThread().getId() + i + ".pm"; 30 31 LOG.info("[Path]: " + directory + "/" + fileName); 32 33 try { 34 // Create file (path: directory/fileName) 35 Files.createFile(Paths.get(directory, fileName)); 36 } catch (IOException ioe) { 37 ioe.printStackTrace(); 38 } 39 } 40 } 41 42 @Override 43 public void run() { 44 this.generator(directory); 45 } 46 }
1 package shuai.study.file.monitor; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.commons.io.FileUtils; 7 import org.apache.commons.io.FilenameUtils; 8 import org.apache.commons.io.filefilter.FileFilterUtils; 9 import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; 10 import org.apache.commons.io.monitor.FileAlterationMonitor; 11 import org.apache.commons.io.monitor.FileAlterationObserver; 12 import org.apache.log4j.Logger; 13 14 import shuai.study.file.tooler.CommonTooler; 15 16 /** 17 * @author shengshu 18 * 19 */ 20 public class FileMonitor extends FileAlterationListenerAdaptor { 21 private static final Logger LOG = Logger.getLogger(FileMonitor.class); 22 23 private static FileMonitor fileMonitor; 24 25 private static int pageNumber = 0; 26 27 private FileMonitor() { 28 29 } 30 31 // Get singleton object instance 32 public static FileMonitor getFileMonitor() { 33 if (fileMonitor == null) { 34 synchronized (FileMonitor.class) { 35 if (fileMonitor == null) { 36 fileMonitor = new FileMonitor(); 37 } 38 } 39 } 40 41 return fileMonitor; 42 } 43 44 // Create file event 45 @Override 46 public void onFileCreate(File file) { 47 LOG.info("[Create]: " + file.getAbsolutePath()); 48 49 String fileAbsolutePath = file.getAbsolutePath(); 50 String fileAbsoluteParentPath = file.getParent(); 51 String fileBaseName = FilenameUtils.getBaseName(fileAbsolutePath); 52 53 File destFile = new File(fileAbsoluteParentPath + File.separator + fileBaseName + "-" + CommonTooler.pagingFormat(pageNumber) + ".xml"); 54 55 try { 56 FileUtils.moveFile(file, destFile); 57 } catch (IOException ioe) { 58 ioe.printStackTrace(); 59 } 60 61 pageNumber++; 62 } 63 64 // Change file event 65 @Override 66 public void onFileChange(File file) { 67 LOG.info("[Change]: " + file.getAbsolutePath()); 68 } 69 70 // Delete file event 71 @Override 72 public void onFileDelete(File file) { 73 LOG.info("[Delete]: " + file.getAbsolutePath()); 74 } 75 76 public void monitor(String directory, int interval) { 77 // Observer file whose suffix is pm 78 FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(directory, FileFilterUtils.and(FileFilterUtils.fileFileFilter(), 79 FileFilterUtils.suffixFileFilter(".pm")), null); 80 81 // Add listener for event (file create & change & delete) 82 fileAlterationObserver.addListener(this); 83 84 // Monitor per interval 85 FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(interval, fileAlterationObserver); 86 87 try { 88 // Start to monitor 89 fileAlterationMonitor.start(); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 } 94 }
1 package shuai.study.file.tester; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Paths; 6 7 import org.apache.log4j.Logger; 8 9 import shuai.study.file.generator.FileGenerator; 10 import shuai.study.file.monitor.FileMonitor; 11 12 /** 13 * @author shengshu 14 * 15 */ 16 public class FileTester { 17 private static final Logger LOG = Logger.getLogger(FileTester.class); 18 19 private static String directory = "/home/omc/tmp"; 20 21 public static void main(String[] args) { 22 // Create directory if it does not exist 23 try { 24 Files.createDirectory(Paths.get(directory)); 25 } catch (IOException ioe) { 26 ioe.printStackTrace(); 27 } 28 29 LOG.info("File Monitor..."); 30 // Start to monitor file event per 500 milliseconds 31 FileMonitor.getFileMonitor().monitor(directory, 500); 32 33 LOG.info("File Generater..."); 34 // New runnable instance 35 FileGenerator fileGeneratorRunnable = new FileGenerator(directory); 36 37 // Start to multi-thread for generating file 38 for (int i = 0; i < 10; i++) { 39 Thread fileGeneratorThread = new Thread(fileGeneratorRunnable); 40 fileGeneratorThread.start(); 41 } 42 } 43 44 }
1 package shuai.study.file.tooler; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 /** 7 * @author shengshu 8 * 9 */ 10 public class CommonTooler { 11 12 // Date time format 13 public static String getDateTimeFormat(Date date) { 14 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmm"); 15 String dateTime = simpleDateFormat.format(date); 16 17 return dateTime; 18 } 19 20 // Page format 21 public static String pagingFormat(int pageNumber) { 22 String pageNumberStr = "P00"; 23 24 if (pageNumber < 0) { 25 throw new NumberFormatException("The page number should not be negative"); 26 } 27 28 if (pageNumber < 10) { 29 pageNumberStr = String.format("P0%d", pageNumber); 30 } else { 31 pageNumberStr = String.format("P%d", pageNumber); 32 } 33 34 return pageNumberStr; 35 } 36 37 }
标签:
原文地址:http://www.cnblogs.com/tinyking/p/4500781.html