Is it possible to get the total count of files inside a folder (and all sub* folders) without to iterate over all files, and go recursivly deeper?
i mean this is very easy indeed but maybe there is a better solution?
private int totalFileCounter = 0;
private void countFiles(File f) {
if (f.isDirectory()) {
for (File fi : f.listFiles()) {
countFiles(fi);
}
} else {
totalFileCounter++;
}
}
edit: okay maybe my question wasnt that good asked… i mean is there any functionality from java or the filesystem or something else that allows me to get the total count of file in O(1)? All the solutions if have now have runtime O(n).
Generally the simplest solutions are the best. I wouldn’t use a field to compute the result of what should be a function. If the method were called from two threads at once you can get incorrect results.