Java file changes monitor folder using jnotify

https://blog.csdn.net/codepython/article/details/42341243?utm_source=blogxgwz1

Use jnotify

https://blog.csdn.net/stalin_/article/details/80234979

Resources added maven import

When developing the jnotify_64bit.dll (64-bit systems with) thrown into the jdk jre bin directory folder such as C: \ Program Files \ Java \ jdk1.8.0_201 \ jre \ bin under

 If you are deploying, reference rxtx article https://www.cnblogs.com/jnhs/p/10274667.html

Dll should be placed in C: \ WINDOWS \ system32 available at

Create a util package

Create a new class Jnotify

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hs.util;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyAdapter;
import net.contentobjects.jnotify.JNotifyException;

public class Jnotify extends JNotifyAdapter {

    private static final String REQUEST_BASE_PATH = "D:\\csdnautodl\\tempfile";
    /**
     * 被监视的目录
     */
    String path = REQUEST_BASE_PATH;
     / ** 
     * Watch event directory 
     * / 
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
     / ** 
     * monitor whether subdirectories that cascade monitor 
     * / 
    boolean watchSubtree = to true ;
     / ** 
     * listener Id 
     * / 
    public  int watchID; 

    / ** 
     * when the container is started to start monitoring program 
     * 
     * 
     * / 
    public  void beginWatch () {
         / ** 
         * Add to monitor queue 
         * / 
        the try {
             the this= JNotify.addWatch .watchID (path, mask, watchSubtree, the this ); 
            System.err.println ( "jnotify ----------- ----------- started successfully" ) ; 
        } the catch (JNotifyException E) { 
            e.printStackTrace (); 
        } 
        / ** 
         * infinite loop, the thread has been executed, sleep a minute later to continue, mainly to make the main thread has time and sleep efficiency occurred without monitoring file 
         * (a directory that is not monitored file changes detected after one minute, almost real-time monitoring, the system calls the local repository) 
         * 
         * / 
        the while ( to true ) {
             the try { 
                the Thread.sleep ( 60000 ); 
            } the catch (InterruptedException E) { //The ignore 
            } 
        } 
    } 

    / ** 
     * When listening catalog Once a new file is created, it will trigger the event 
     * 
     * @param WD listening thread the above mentioned id 
     * @param rootPath monitor directory 
     * @param name the file name
      * / 
    @Override 
    public  void fileCreated ( int WD, rootPath String, String name) { 
        System.err.println ( "file is created, the position is created:" + rootPath + "/" + name); 
    } 

    @Override 
    public  void fileRenamed ( int WD, String rootPath, String oldName, String newName ) {
        System.err.println ( "file is renamed, the original file name:" + rootPath + "/" + in oldName
                 + ", now file name:" + rootPath + "/" + newName); 
    } 

    @Override 
    public  void fileModified ( int WD, rootPath String, String name) { 
        System.err.println ( "file contents have been modified, file name:" + rootPath + "/" + name); 
    } 

    @Override 
    public  void fileDeleted ( int WD, String rootPath, String name) { 
        System.err.println ( "file is deleted, the deleted file name:" + + rootPath name); 
    } 
}

 

 Then started monitoring in the form of program execution time

 

Guess you like

Origin www.cnblogs.com/jnhs/p/11417078.html