gpt4 book ai didi

java - 如何使用 Java 索引整个硬盘/文件系统?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:19 25 4
gpt4 key购买 nike

我想使用Java来获取整个硬盘上的文件更改。例如c:\或/mnt/drives/hdd1

这是一个要求,因为许多不同的计算机使用不同的文件结构,不能轻易更改而不影响其他软件。但应该对特定文件和文件类型建立索引。它们可以存在于驱动器 c:\d:\e:\和任何子文件夹中。

Java WatchService 无法完成这项工作,因为您必须手动添加每个子目录。文件夹数量超过 10+k,这是不可行的,而且速度很慢。

我正在 JAVA 中寻找类似的东西:

C# 和 Java 实现之间的区别:如果我使用管理员权限运行 WatchService Java 代码,我可以访问 c:\$Recycle.Bin,但不能访问 c:\Documents and Settings。我收到拒绝访问异常。有人能告诉我为什么吗?正如我提到的,WatchService 不是解决方案,因为它需要花费大量时间才能爬取所有子目录。对于 10,000 多个文件夹来说,注册每个子文件夹并使用成对的 WatchKey 和 java.nio2.Path 维护一个 Map 是一个非常糟糕的解决方案。

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;
}
});
}

最佳答案

对我来说,这看起来像是 Monitor subfolders with a Java watch service 的重复项

您可以像这样观看子目录:

/**
* Register the given directory, and all its sub-directories, with the WatchService.
*/
private void registerAll(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 {
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
});
}

关于java - 如何使用 Java 索引整个硬盘/文件系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24891509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com