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

      

  • 相关阅读:
    OO第四单元作业总结暨完结撒花
    OO第三单元作业总结【自我审判】
    菜鸡学C语言之知识点简单整理
    菜鸡学C语言之混凝土(四柱汉诺塔)
    OO第二单元作业总结【自我反思与审视】
    菜鸡学C语言之寻根溯源
    菜鸡学C语言之真心话大冒险
    菜鸡学C语言之摸鱼村村长
    OO面向对象第一单元总结
    day10 python全栈学习笔记
  • 原文地址:https://www.cnblogs.com/minglie/p/Sample.html
Copyright © 2011-2022 走看看