How To Monitor Win32 File System Changes in Java (300605)



The information in this article applies to:

  • Microsoft SDK for Java 4.0
  • Microsoft Visual J++ 6.0

This article was previously published under Q300605

SUMMARY

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

MORE INFORMATION

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 Class1.java in Visual J++ 6.0.
  2. Replace the default Class1 code that Visual J++ generates with the following code:
    import com.ms.wfc.core.*;
    
    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));
    		f.run();
    	}
    	
    	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] = com.ms.win32.Kernel32.FindFirstChangeNotification("c:\\",true,com.ms.win32.winf.FILE_NOTIFY_CHANGE_FILE_NAME);
    		
    		while(true) {
    			int waitStatus = com.ms.win32.Kernel32.WaitForMultipleObjects(handles.length,handles,false,com.ms.win32.wini.INFINITE);
    			
    			switch(waitStatus) {
    			case com.ms.win32.winw.WAIT_OBJECT_0:
    				OnFileSystemEvent(new Event());
    				com.ms.win32.Kernel32.FindNextChangeNotification(handles[0]);
    				break;
    			}
    		}
    	}
    	
    	public final void addOnFileSystemEvent(EventHandler handler) {
    		fileEventDelegate = (EventHandler)com.ms.lang.Delegate.combine(fileEventDelegate,handler);
    	}
    	protected void OnFileSystemEvent(Event event) {
    		if(fileEventDelegate!=null){
    			fileEventDelegate.invoke(this,event);
    		}
    	}
    } 
    					
  3. Save Class1.java.
  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- class1.java
    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
    						


Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto KB300605