Java Code Examples for org.apache.commons.io.filefilter.FileFilterUtils#or()

The following examples show how to use org.apache.commons.io.filefilter.FileFilterUtils#or() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
/**
 * 只监控文件发送变化,如果是子目录的文件改变,则目录会变,由于没有过滤目录发生变化,则目录下的文件改变不会监控到
 * @param dir
 * @throws Exception 
 */
public void check1(String dir) throws Exception
{
	File directory = new File(dir);
    // 轮询间隔 5 秒
    long interval = TimeUnit.SECONDS.toMillis(5);
    // 创建一个文件观察器用于处理文件的格式
    IOFileFilter filter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
    		FileFilterUtils.suffixFileFilter(".jar"));
    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
    //设置文件变化监听器
    observer.addListener(new MyFileListener());
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
    monitor.addObserver(observer);//文件观察
    monitor.start();
}
 
Example 2
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void check2(String dir) throws Exception
	{
		File directory = new File(dir);
	    // 轮询间隔 5 秒
	    long interval = TimeUnit.SECONDS.toMillis(5);
	    //后缀过滤器
	    IOFileFilter filefilter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
	    		FileFilterUtils.suffixFileFilter(".jar"));
	    //子目录的后缀
	    IOFileFilter subFilefilter=FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),filefilter);
	    //根目录和子目录变化
	    IOFileFilter filter = FileFilterUtils.or(filefilter,subFilefilter);
	    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
	    //设置文件变化监听器
	    observer.addListener(new MyFileListener());
	    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
	    monitor.addObserver(observer);//文件观察
	    monitor.start();
//	    monitor.addObserver(observer);//文件观察,如果在start后面加,则会触发所有文件创建
	}
 
Example 3
Source File: DefaultClassSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected IOFileFilter buildFilterBySuffixs(String directory,String ...suffixs)
{
    IOFileFilter iOFileFilter=FileFilterUtils.directoryFileFilter(); //子目录变化
	for(String suffix:suffixs)
	{//后缀过滤器
		iOFileFilter=FileFilterUtils.or(iOFileFilter,FileFilterUtils.suffixFileFilter(suffix));
	}
    return iOFileFilter;
}
 
Example 4
Source File: DefaultClassSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected IOFileFilter buildFilterByNames(String directory,String ...fileName)
{
    IOFileFilter iOFileFilter=FileFilterUtils.directoryFileFilter(); //子目录变化
	for(String name:fileName)
	{//名字过滤器
		iOFileFilter=FileFilterUtils.or(iOFileFilter,FileFilterUtils.nameFileFilter(name));
	}
    return iOFileFilter;
}
 
Example 5
Source File: DefaultScriptSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected FileAlterationObserver buildFileAlterationObserver(String directory)
{
    //后缀过滤器
    IOFileFilter suffixFileFilter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),FileFilterUtils.suffixFileFilter(".jar"));
    //子目录变化
    IOFileFilter rootAndSubFilefilter=FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),suffixFileFilter);
    return new FileAlterationObserver(directory,rootAndSubFilefilter);
}
 
Example 6
Source File: TemplateWatcher.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 * @param manager the templating manager, to which event handling is delegated.
 * @param templateDir the templates directory to watch.
 * @param pollInterval the poll interval.
 * @throws IOException if there is a problem watching the template directory.
 */
public TemplateWatcher( final TemplatingManager manager, final File templateDir, final long pollInterval ) {
	this.templateDir = templateDir;

	// Register the custom helpers.
	this.handlebars.registerHelper( AllHelper.NAME, new AllHelper());
	this.handlebars.registerHelper( IsKeyHelper.NAME, new IsKeyHelper());

	// Pretty formatting
	this.handlebars.prettyPrint( true );

	// Create the observer, register this object as the event listener.
	FileFilter fileFilter = FileFilterUtils.or(
			FileFilterUtils.and(
					FileFilterUtils.fileFileFilter(),
					FileFilterUtils.suffixFileFilter( ".tpl" ),
					CanReadFileFilter.CAN_READ,
					new TemplateFileFilter(templateDir)),
			FileFilterUtils.and(
					FileFilterUtils.directoryFileFilter(),
					CanReadFileFilter.CAN_READ,
					new TemplateSubDirectoryFileFilter(templateDir))
	);

	FileAlterationObserver observer = new FileAlterationObserver( this.templateDir, fileFilter );
	observer.addListener( this );

	// Create the monitor.
	this.monitor = new FileAlterationMonitor( pollInterval, observer );
	this.monitor.setThreadFactory( THREAD_FACTORY );

	this.manager = manager;
	this.logger.fine( "Template watcher is watching "
			+ this.templateDir
			+ " with an interval of " + pollInterval + " ms." );
}
 
Example 7
Source File: DirectoryWalker.java    From aion-germany with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Construct an instance with a directory and a file filter and an optional
 * limit on the <i>depth</i> navigated to.
 * <p>
 * The filters control which files and directories will be navigated to as part
 * of the walk. This constructor uses {@link FileFilterUtils#makeDirectoryOnly(IOFileFilter)}
 * and {@link FileFilterUtils#makeFileOnly(IOFileFilter)} internally to combine the filters.
 * A {@code null} filter means that no filtering should occur.
 *
 * @param directoryFilter  the filter to apply to directories, null means visit all directories
 * @param fileFilter  the filter to apply to files, null means visit all files
 * @param depthLimit  controls how <i>deep</i> the hierarchy is
 *  navigated to (less than 0 means unlimited)
 */
protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, int depthLimit) {
    if (directoryFilter == null && fileFilter == null) {
        this.filter = null;
    } else {
        directoryFilter = directoryFilter != null ? directoryFilter : TrueFileFilter.TRUE;
        fileFilter = fileFilter != null ? fileFilter : TrueFileFilter.TRUE;
        directoryFilter = FileFilterUtils.makeDirectoryOnly(directoryFilter);
        fileFilter = FileFilterUtils.makeFileOnly(fileFilter);
        this.filter = FileFilterUtils.or(directoryFilter, fileFilter);
    }
    this.depthLimit = depthLimit;
}
 
Example 8
Source File: DirectoryWalker.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Construct an instance with a directory and a file filter and an optional
 * limit on the <i>depth</i> navigated to.
 * <p>
 * The filters control which files and directories will be navigated to as part
 * of the walk. This constructor uses {@link FileFilterUtils#makeDirectoryOnly(IOFileFilter)}
 * and {@link FileFilterUtils#makeFileOnly(IOFileFilter)} internally to combine the filters.
 * A {@code null} filter means that no filtering should occur.
 *
 * @param directoryFilter  the filter to apply to directories, null means visit all directories
 * @param fileFilter  the filter to apply to files, null means visit all files
 * @param depthLimit  controls how <i>deep</i> the hierarchy is
 *  navigated to (less than 0 means unlimited)
 */
protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, final int depthLimit) {
    if (directoryFilter == null && fileFilter == null) {
        this.filter = null;
    } else {
        directoryFilter = directoryFilter != null ? directoryFilter : TrueFileFilter.TRUE;
        fileFilter = fileFilter != null ? fileFilter : TrueFileFilter.TRUE;
        directoryFilter = FileFilterUtils.makeDirectoryOnly(directoryFilter);
        fileFilter = FileFilterUtils.makeFileOnly(fileFilter);
        this.filter = FileFilterUtils.or(directoryFilter, fileFilter);
    }
    this.depthLimit = depthLimit;
}
 
Example 9
Source File: FileMonitorTest.java    From spring-boot with Apache License 2.0 3 votes vote down vote up
/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {


    // 监控根目录,会递归监控子文件夹变化

    String rootDir = "D:\\download";
    // 轮询间隔 5 秒,通过重新启动 FileAlterationMonitor 实现
    long interval = TimeUnit.SECONDS.toMillis(5);


    // 创建一个文件观察器用于处理文件的格式
    // Create a FileFilter
    IOFileFilter directories = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE);
    IOFileFilter files = FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".java"));
    IOFileFilter filter = FileFilterUtils.or(directories, files);

    // Create the File system observer and register File Listeners
    FileAlterationObserver observer = new FileAlterationObserver(rootDir, filter, null);
    observer.addListener(new FileMonitorFileListener()); //设置文件变化监听器

    //创建文件变化监听器
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);

    // 开始监控
    monitor.start();

}