Directory Counter in Java

Here is a simple example for directory counter. This example will just count the directory/folders where the .class file be. For example the .class will be in the root directory or in C:/ directory it will count all the folders in it excepts sub-folders.

import java.io.File;

public class DirCount{
static int count = 0;
public static void main(String[] args){
if(args.length == 0){
System.out.println("Usage : DirCount <FolderPath>");
System.exit(0);
}
try{
File f = new File(args[0]);
File[] fs = f.listFiles();
for(File ff : fs){
if(ff.isDirectory()){
count++;
}
}
System.out.println("Total Numbers of directories : " + count);
}catch(Exception ex){
System.out.println("Provide proper path");
}
}
}

Write the above program and save it as DirCount.java.
Now compile the program by command javac DirCount.java and execute the class file by java DirCount

No comments:

Post a Comment