• 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();
        }
    }
    

      

  • 相关阅读:
    coredump文件设置及调试
    github上传本地代码库步骤
    ubuntu上SVN版本升级到1.7
    ubuntu 上samba创建共享组目录
    linux下创建只有某个用户组可用的文件夹
    usermod -a表示在原来所属组的基础上追加
    linux mount
    Ubuntu Bash and Dash
    svn co 默认密钥' GNOME keyring
    精简版ffmpeg编译脚本
  • 原文地址:https://www.cnblogs.com/minglie/p/Sample.html
Copyright © 2020-2023  润新知