How To Monitor Win32 File System Changes in Java


This article describes how you can use the Win32 FindFirstChangeNotification function and its related functions to monitor file system changes from an application.


In this section, you create a Java application that monitors the file system for file name changes. The Java application code uses the Microsoft Java Delegate mechanism to create a simple event that is named fileEvent. This event is triggered by the underlying file system events. The code creates a Java Console application that creates a thread to monitor the file system. This thread displays messages in the Java Console window when a file change notification event is triggered.

  1. Create a new Console application project named in Visual J++ 6.0.
  2. Replace the default Class1 code that Visual J++ generates with the following code:
    import*;public class Class1{public static void main (String[] args) {Class1 c = new Class1();c.start();}public void start() {System.out.println("Starting Thread");FileSystemCtrl f = new FileSystemCtrl();f.addOnFileSystemEvent(new EventHandler(this.fileEvent));;}public void fileEvent(Object source, Event e) {System.out.println("FileEvent Handled");}}class FileSystemCtrl extends Thread {EventHandler fileEventDelegate = null;public void run() {int[] handles = new int[1];handles[0] ="c:\\",true,;while(true) {int waitStatus =,handles,false,;switch(waitStatus) {case Event());[0]);break;}}}public final void addOnFileSystemEvent(EventHandler handler) {fileEventDelegate = (EventHandler),handler);}protected void OnFileSystemEvent(Event event) {if(fileEventDelegate!=null){fileEventDelegate.invoke(this,event);}}} 
  3. Save
  4. At a command prompt, run the following commands (which use the tools in the Microsoft Software Development Kit for Java) to build and to execute the code:
    jvc /nomessage /x-
    start jview class1
  5. Click Start, and then click Run.
  6. Type Notepad C:\Test.txt, and then click OK.
  7. When you are prompted to create a new file, click OK. Notice that the following message appears in the Java Console window:

    FileEvent Handled

ID articol: 300605 - Ultima examinare: 14 feb. 2017 - Revizie: 1

Microsoft Software Development Kit pentru Java 4.0, Microsoft Visual J++ 6.0 Standard Edition