zoukankan      html  css  js  c++  java
  • Java监听文件变化

    package com.aliyun.FileLinster;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.WatchEvent;
    import java.nio.file.WatchKey;
    import java.nio.file.WatchService;
    import static java.nio.file.StandardWatchEventKinds.*;
    
    public class Sample {
    
        private WatchService watcher;
    
        private Path path;
    
        public Sample(Path path) throws IOException {
            this.path = path;
            watcher = FileSystems.getDefault().newWatchService();
            this.path.register(watcher, OVERFLOW, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        }
    
        public void handleEvents() throws InterruptedException {
            // start to process the data files
            while (true) {
                // start to handle the file change event
                final WatchKey key = watcher.take();
    
                for (WatchEvent<?> event : key.pollEvents()) {
                    // get event type
                    final WatchEvent.Kind<?> kind = event.kind();
    
                    // get file name
                    @SuppressWarnings("unchecked")
                    final WatchEvent<Path> pathWatchEvent = (WatchEvent<Path>) event;
                    final Path fileName = pathWatchEvent.context();
    
                    if (kind == ENTRY_CREATE) {
    
                        System.out.println("ENTRY_CREATE");
                        // 说明点1
                        // create a new thread to monitor the new file
                        new Thread(new Runnable() {
                            public void run() {
                                File file = new File(path.toFile().getAbsolutePath() + "/" + fileName);
                                boolean exist;
                                long size = 0;
                                long lastModified = 0;
                                int sameCount = 0;
                                while (exist = file.exists()) {
                                    // if the 'size' and 'lastModified' attribute keep same for 3 times,
                                    // then we think the file was transferred successfully
                                    if (size == file.length() && lastModified == file.lastModified()) {
                                        if (++sameCount >= 3) {
                                            break;
                                        }
                                    } else {
                                        size = file.length();
                                        lastModified = file.lastModified();
                                    }
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        return;
                                    }
                                }
                                // if the new file was cancelled or deleted
                                if (!exist) {
                                    return;
                                } else {
    
                                } 
                            }
                        }).start();
                    } else if (kind == ENTRY_DELETE) {
                        System.out.println("ENTRY_DELETE");
                    } else if (kind == ENTRY_MODIFY) {
                        System.out.println("ENTRY_MODIFY");
                    } else if (kind == OVERFLOW) {
                        System.out.println("OVERFLOW");
                    }
                }
    
                // IMPORTANT: the key must be reset after processed
                if (!key.reset()) {
                    return;
                }
            }
        }
    
        public static void main(String args[]) throws IOException, InterruptedException {
            new Sample(Paths.get("D:\T")).handleEvents();
        }
    }
    

      

  • 相关阅读:
    配置Yaf
    计算机科学中最重要的32个算法
    mysql show status详解
    Structs 在Struts.xml中配置action时,action的name属性最好首字母大写
    MyEclipse创建ssh项目和连接数据库
    Myeclipse安装svn插件
    win7安装ubuntu双系统
    Java查看API和源码的方法
    华为oj平台的新网址
    详细解析Java中抽象类和接口的区别
  • 原文地址:https://www.cnblogs.com/minglie/p/Sample.html
Copyright © 2011-2022 走看看