B4J Library jFileWatcher - watch system file events

This library wraps many capabilities from the java.nio.file package and a few from the java.io.File package. It allows you to set certain directories to be watched, raising events when files or folders within those directories are created, deleted or modified. Additionally, this library enables you to get and set last access time, last modified time, creation time, read-only state and hiddenness of files. Beware, some operating systems won't support all of those attributes.

On Windows, this library performs quite well, using the system's native file events and consuming only a minuscule amount of system resources. The latency was also extremely low. On systems that don't have native file events, the library will poll the file system for changes, periodically. This might introduce a latency in receiving file change events and might consume more resources, though I haven't tested it on any such systems.

The watching is performed off the main thread however events are raised on the main thread.

Example code:
Sub Process_Globals
    Dim fw As FileWatcher
    Dim t as Timer
End Sub

Sub AppStart(Args() as String)
     t.Initialize("t", 60*1000)   'Stop file watching in 1 minute
     fw.Initialize("fw")     'Initialize with the event name
     fw.SetWatchList(Array as String(File.DirApp))   'Set the current dir to be watched
     fw.StartWatching       'Start Watching
     t.Enabled = true        'Start the timer

     Log("creation time: " & fw.GetCreationTime("C:\Test\test.txt"))   'Log creation time of a file
     fw.SetCreationTime("C:\Test\test.txt", DateTime.Now)    'Set the creation time to now
     Log("creation time: " & fw.GetCreationTime("C:\Test\test.txt"))   'Log the new creation time of file
End Sub

Sub fw_CreationDetected(FileName As String)
    Log("CreationDetected: " & FileName)    'Logs the creation of a new file or folder
End Sub

Sub fw_DeletionDetected(FileName As String)
    Log("DeletionDetected: " & FileName)   'Logs the deletion of a file or folder
End Sub

Sub fw_ModificationDetected(FileName As String)
    Log("ModificationDetected: " & FileName)   'Logs the modification of a file or folder
End Sub

Sub fw_WatchingTerminated   'Logs the termination of the FileWatcher
    Log("WatchingTerminated")   'The FileWatcher can still be used, it just has to be started again.
    StopMessageLoop     'With the main message loop ended and the FileWatcher
                               'watcher thread ended, this application will close now.
End Sub

Sub t_Tick
     Log("Timer Ticked, watching stopped")
End Sub

This library was compiled against jdk 8.

EDIT(26JUL2015): Updated to version 1.1. See post #3 for details.
EDIT(2AUG2015): Updated to version 1.2. See post #8 for details.
EDIT(06MAR2018): Updated to version 1.3. See post #21 for details.
EDIT(23APR2020): Updated to version 1.4. See post #28 for details.


  • jFileWatcher.zip
    5.7 KB · Views: 578
  • jFileWatcher1.1.zip
    5.8 KB · Views: 463
  • jFileWatcher1.2.zip
    5.9 KB · Views: 705
  • jFileWatcher1.3.zip
    8 KB · Views: 533
  • jFileWatcher1.4.zip
    8.1 KB · Views: 431
Last edited:

John Naylor

Active Member
Licensed User
Longtime User
"Turn off sleep / hybernate mode" that would work same as my file.exists in a loop in a timer would work. i can program around the problem. i'm more asking if the problem could be fixed at the library level in the first place. thats why i asked on this old thread, if it can be fixed thats great if it can't be fixed then anyone (like me) who wants to use this library should be aware that if your watching for a file event to occur on another computer across the network and your watching computer can go to sleep, be aware that it will be missing file events. if i had known that i would not have used the lib. remember i did say "noob here". hoping roycefer will weight in here. maybe i could beg for source code ?? as if noob could do something.
Can you even receive a file whilst in sleep mode? I wasn't aware that you could as I only use jfilewatcher on a permanently awake VPS


Well-Known Member
Licensed User
Longtime User
I'm not sure anything can be done while the system is sleeping. Isn't that the point of sleeping?

The jFileWatcher library uses the OS's file watching mechanisms. The library has no control over if the watching is done with polling or native file watching events.
ultimately i'm using the wrong tool for the job. i'm using jfilewatcher as a form of communications between computers. doing something like this "if this file exists on server then do this, or if this file exist then do that". in my case if file gate168.txt exists then change your gateway address to if file gate167.txt exists then change your gateway address to the server is creating files as a way to communicate with all the other computers on the network. the server from time to time checks the primary internet, if its down then it creates a file on the server (it never sleeps) to tell the other computers what to do. i thought maybe there was something you could add to the libaray but it i guess not. problem is solved for me: i use both polling and jfilewatcher. polling by using file.exists in a timer. polling is sure but jfilewatcher is fast. im done with the program and its deployed, no problems so far, i've moved on to b4j and jopencv. one day i'll have to come back to this program and add new things i'll use a better tool for the job next time. mqtt looks like it might be what i'm looking for.