Last active
January 4, 2016 08:18
-
-
Save toboqus/ab74343c9ee46cd61cc5 to your computer and use it in GitHub Desktop.
counting number of files within a given directory (Breadth first)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int count(File root){ | |
if(root == null || !root.exists()){ | |
System.out.println("ERROR: The source drive/directory does not exist."); | |
return -1; | |
} | |
Queue<File> files = new Queue<>(); | |
files.push(root); | |
int sum = 0; | |
while(!files.empty()){ | |
File tmp = files.pop(); | |
if(tmp.isFile()){ | |
sum++; | |
}else if(tmp.isDirectory()){ | |
for(File f : tmp.listFiles()){ | |
files.push(f); | |
} | |
} | |
} | |
return sum; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment