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

时间:2014-07-22 15:27:59

标签: java c# indexing filesystems watchservice

我想使用Java在整个硬盘上进行文件更改。 例如c:\或/ mnt / drives / hdd1

这是一项要求,因为许多不同的计算机使用不同的文件结构,在不影响其他软件的情况下无法轻易更改。但应将特定文件和文件类型编入索引。它们可以存在于驱动器c:\ d:\ e:\和任何子文件夹中。

Java WatchService不能完成这项工作,因为您必须手动添加每个子目录。超过10 + k个文件夹是不可行的和慢的。

我在JAVA中搜索类似的内容:

C#和Java实现之间的区别: 如果我使用Admin Privledges运行WatchService Java代码,我可以访问c:\ $ Recycle.Bin但不能访问c:\ Documents and Settings。我得到一个拒绝访问权限的例外。有人可以告诉我为什么吗?正如我所提到的,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;
        }
    });
}

1 个答案:

答案 0 :(得分:1)

对我而言,这看起来像是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;
        }
    });
}