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 verifying each path independently like this:
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 that I don't create 50,000 File objects ? I am also using guava. Is there any utility in there which can help me with bulk exists()
method ?
isfile() method checks if a file exists in Python. os. path. isfile() returns True or False, depending on whether that file can be found.
To test to see if a file or directory exists, use the exists method of the Java File class, as shown in this example: File tmpDir = new File("/var/tmp"); boolean exists = tmpDir. exists(); The existing method of the Java File class returns true if the file or directory exists, and false otherwise.
The creation of 50,000 File
objects is almost certainly not the bottleneck. The actual filesystem operations is probably what's making it slow.
I have two suggestions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With