How can I check if files exist for a given path list?

I have a list of 50,000 paths, and I need to check if a file exists against each of these paths. Right now, I am checking each path independently as follows:

public static List<String> filesExist(String baseDirectory, Iterable<String> paths) throws FileNotFoundException{
        File directory = new File(baseDirectory);
        if(!directory.exists()){
            throw new FileNotFoundException("No Directory found: " + baseDirectory );
        }else{
            if(!directory.isDirectory())
                throw new FileNotFoundException(baseDirectory + " is not a directory!");
        }

        List<String> filesNotFound = new ArrayList<String>();

        for (String path : paths) {
            if(!new File(baseDirectory + path).isFile())
                filesNotFound.add(path);
        }
        return filesNotFound;
    }

Is there a way to improve it so as not to create 50,000 File objects? I also use guava. Is there any usefulness that can help me with the mass method exists()?

+5
source share
5 answers

Creating objects of size 50,000 is Filealmost certainly not a bottleneck. In fact, file system operations are likely to slow things down.

I have two suggestions:

  • , .
  • , , .
+5

aix, . , IF, baseDirectory ( ), FileUtils.iterateFiles FileUtils.listFiles, , . , , , , , .

, , .

( aix-, ...)

0

IMHO ( ) :

  • -
  • , "" , ""
  • , , .

( ):

tree1: / /a /a/a /d /d/a /d/a/b /e
tree2: / /a /b /d /d/a /e

:

  • /
  • /a,
  • /a/a tree2
  • /b tree1
  • /d,
  • ...

filesNotFound , , .

0

- , . Go-go gadget!

public static List<String> filesExist(String baseDirectory, Iterable<String> paths) throws FileNotFoundException{
    final File base = new File(baseDirectory);
    if (base.exists()) {
        return FluentIterable.from(paths).filter(new Predicate<String>() {
            public boolean apply(String in) {
                return new File(in,base).exists();
            }
        }).toImmutableList();
    }
    throw new FileNotFoundException("Base doesn't exist!");
}

, -.

0

.

TRIE

enter image description here

, . . , .

Total Operation = Total Node -

. , , Guava, .

0

All Articles