I have a windows directory tree with about 1,000,000 files inside. I have a text file that i read in Java, contains some file names (about 100,000), and I want to check for every file name - if it exists in the directory (if yes - give me a full path of the file).

Already tried those options:

1.

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}

2.

public void func(String path, String name)
{

    Path folder = Paths.get(path);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder))
    {
        for (Path entry : stream) 
        {
            if(Files.isDirectory(entry))
            {
                func(entry.toString(), name);
            }
            else
            {
                if(FilenameUtils.removeExtension(entry.getFileName().toString()).equals(name))
                {
                    System.out.println(entry);
                }
            }
        }
    } 
    catch (IOException ex) {
    // An I/O problem has occurred
    }

}

So far all those options to do so are very slow. As I guess, although all the files are in the same logic place, actually every file is saved in another place in the hard drive, so all those IO calls take too much time.

Another idea that i found here is ISearchFolderItemFactory interface, but I found documentation for it only in C++, not in Java.

Maybe I can implement a pre-sort or something, to put all the files really together in the hd, sorted by name, and then to use some hash method to find name by name?

Need some help...

0 Answers