java JNotify (基於內核)實時監控文件

 JNotify 下載地址 

jnotify(包含dll與so).rar

win 將dll文件放入  jdk bin目錄下

linux 將so文件放入  jdk bin目錄下

maven增加JNotify 依賴

		<dependency>
			<groupId>net.contentobjects.jnotify</groupId>
			<artifactId>jnotify</artifactId>
			<version>0.94</version>
		</dependency>

工具編寫

首先在D盤下創建test文件夾

然後執行以下程序

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicInteger;

public class Jnotify {

    public static void main(String[] args) throws Exception {

        // 設置監聽的文件夾,或者文件
        String linstenerPath = "D:" + File.separator + "test";

        // 指定監聽的模式,創建、刪除、修改、和重命名
        int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;

        // 是否監控子目錄
        boolean watchSubtree = true;

        // 設置監聽器,得到一個監聽器id,可以通過這個id來移除監聽器
        int watchId = JNotify.addWatch(linstenerPath, mask, watchSubtree, new MyJnotifyListner());

        // 如果需要去掉監聽器,比如不需要監聽某個目錄了
        //JNotify.removeWatch(watchId);

        //啓動線程,不斷的創建、重命名和刪除文件,測試jnotify的響應速度
        new DynamicBuildFile(linstenerPath).start();

        // 一定要讓程序等待,只要程序不退出,那麼上面的監聽器就一直有效
        Thread.sleep(Integer.MAX_VALUE);
    }

    static class MyJnotifyListner implements JNotifyListener {
        AtomicInteger integer = new AtomicInteger(0);

        @Override
        public void fileCreated(int i, String rootPath, String name) {
            System.out.println("創建文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileDeleted(int i, String rootPath, String name) {
            System.out.println("刪除文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileModified(int i, String rootPath, String name) {
            System.out.println("更改文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileRenamed(int i, String rootPath, String oldName, String newName) {
            System.out.println("重命名文件 : " + (rootPath + "/" + oldName) + ";新文件名 : " + (rootPath + "/" + newName) + "integer:" + integer.incrementAndGet());
        }
    }

    /**
     * 動態生成和刪除文件,測試jnotify的響應速度
     * 總結
     * 1、create 只會觸發create
     * 2、delete 會觸發 delete和modified
     * 3、rename 會出發一次rename 和modified
     * 4、如果編輯那個文件,那麼會先產生一個臨時文件(create),然後modified,在然後rename
     */
    static class DynamicBuildFile extends Thread {
        String basePath;

        DynamicBuildFile(String basePath) {
            this.basePath = basePath;
        }

        @Override
        public void run() {
            while (true) {
                File file1 = new File(basePath + File.separator + "test1.txt");
                File file2 = new File(basePath + File.separator + "test2.txt");
                File file3 = new File(basePath + File.separator + "test3.txt");
                try {
                    PrintWriter printWriter1 = new PrintWriter(new FileOutputStream(file1));
                    //下面兩行,都會觸發modify
                    printWriter1.println("測試測試");
                    printWriter1.close();

                    PrintWriter printWriter2 = new PrintWriter(new FileOutputStream(file2));
                    printWriter2.println("測試測試");
                    printWriter2.close();

                    PrintWriter printWriter3 = new PrintWriter(new FileOutputStream(file3));
                    printWriter3.println("測試測試");
                    printWriter3.close();

                    file1.delete();
                    file2.delete();
                    file3.delete();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 使用線程頻繁增刪改測試響應

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章