我想使用 Java 来获取整个硬盘驱动器上的文件更改。例如 c:\ 或 /mnt/drives/hdd1
这是一个要求,因为许多不同的计算机使用不同的文件结构,不能轻易更改而不影响其他软件。但是应该索引特定的文件和文件类型。它们可以存在于驱动器 c:\ d:\ e:\ 和任何子文件夹中。
Java WatchService 无法完成这项工作,因为您必须手动添加每个子目录。超过 10+k 个文件夹是不可行且缓慢的。
我在 JAVA 中寻找这样的东西:
C# 和 Java 实现之间的区别:如果我使用管理员权限运行 WatchService Java 代码,我可以访问 c:\$Recycle.Bin 但不能访问 c:\Documents 和设置。我收到拒绝访问异常。有人可以告诉我为什么吗?正如我提到的 WatchService 不是解决方案,因为它需要很长时间才能爬取所有子目录。注册每个子文件夹并使用成对的 WatchKey 和 java.nio2.Path 维护一个 Map 对于 10,000+ 来说是一个非常糟糕的解决方案文件夹。
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);
}
}
}
JAVA实现:
private void registerDirectoryWithSubfolders(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException
{
System.out.println(dir);
try
{
registerDirectory(dir);
}
catch(java.nio.file.AccessDeniedException ex)
{
System.err.println("Access Denied: " + dir);
}
catch(java.lang.Throwable ex)
{
System.err.println("Exception: " + dir);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException
{
System.err.println("Error And SKIP " + file);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return FileVisitResult.SKIP_SUBTREE;
}
});
}