How to calculate number of files in a directory in java code examples
private static int calNoOfFiles(String directoryPath)
{
String fileDelimiter= File.separator;
File fd = new File(directoryPath);
if(!fd.isFile())
{
String list[] = fd.list();
for(int i = 0; i < list.length; i++)
{
String tempDirPath = directoryPath + fileDelimiter + list[i];
debug(tempDirPath);
File ftd = new File(tempDirPath);
if(ftd.isFile())
noOfFiles++;
if(ftd.isDirectory())
calNoOfFiles(tempDirPath);
}
}
return noOfFiles;
}
{
String fileDelimiter= File.separator;
File fd = new File(directoryPath);
if(!fd.isFile())
{
String list[] = fd.list();
for(int i = 0; i < list.length; i++)
{
String tempDirPath = directoryPath + fileDelimiter + list[i];
debug(tempDirPath);
File ftd = new File(tempDirPath);
if(ftd.isFile())
noOfFiles++;
if(ftd.isDirectory())
calNoOfFiles(tempDirPath);
}
}
return noOfFiles;
}
Comments
Post a Comment