How do I check Total number of directory in java

Updated:13/feb/2020 by Computer Hope

Here we are Showing a method
String [] names=mydir.list();
That is helpfull to get all directory
with this we can doing a looping on this .

import java.io.*;
import java.util.*;
class FileTotalDirectory
{
	public static void main(String args[]) 
{
    //get number of files in a directory and its subdirectories
    File mydir=new File("D:/Assignments");
    String [] names=mydir.list();
    System.out.println("Total memebers in directory "+mydir.getName()+" are "+names.length);
    for(String s:names)
    {
    	System.out.println(s);
    }
    
}
}

Second example to print details of Total directory in java

Here we are Showing a detail of directory using java method

That is helpfull to get all info of directory
with this we can doing a looping on this .

import java.io.*;
import java.util.*;
class FileTotalDirectory
{
	public static void main(String args[]) 
{
    File mydir=new File("D:/Assignments");
    File [] names=mydir.listFiles();
    System.out.println("Total memebers in directory "+mydir.getName()+" are "+names.length);
    for(File s:names)
    {
    	long ms=s.lastModified();
    	Date dt=new Date(ms);
    	if(s.isFile())
    	System.out.println(s.getName()+" is a file and was last mod on "+dt);
    	else
    	System.out.println(s.getName()+" is a directory and was last mod on "+dt);	
    }
    
}
}