I want to use Java to get file changes on the entire hard drive.
e.g. c:\ or /mnt/drives/hdd1
It is a requirement because many different computers are used with different file structure, which cannot be changed easily without having an impact on other software. But specific files and filetypes should be indexed. They can exist on drive c:\ d:\ e:\ and any subfolder.
Java WatchService doesn't do the job because you have to manually add every subdirectory. With over 10+k folders that is infeasible.
I am searching for something like this in JAVA:
Difference between C# and Java implementation: C# outputs even changes in c:$Recycle.Bin\ but Java throws an Exception if i listen for changes in this particular directory.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileSystemWatcherTest
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
try
{
// Watch for changes on this path
fileSystemWatcher.Path = "c:\\";
// Watch for changes on all files
fileSystemWatcher.Filter = "*.*";
// Also watch for changes within sub directories
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Changed += fileSystemWatcher_Changed;
fileSystemWatcher.Created += fileSystemWatcher_Created;
fileSystemWatcher.Deleted += fileSystemWatcher_Deleted;
fileSystemWatcher.Renamed += fileSystemWatcher_Renamed;
// Begin watching
fileSystemWatcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
while (true)
{
System.Threading.Thread.Sleep(60 * 1000);
}
}
static void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("Rename " + e.FullPath);
}
static void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Delete " + e.FullPath);
}
static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Create " + e.FullPath);
}
static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Change " + e.FullPath);
}
}
}