Check this one.
Note: if number of recursive folders are very high, then there is every possibility to get StackOverflow exception.
Regards,
Amar
Main Topics
Browse All TopicsI was wondering what would be the code to implement a recursive function to find the total size of the folder even if it contained subfolders.
if ( file.isDirectory() )
{
File[] list = file.listFiles();
long size=0;
for (int i = 0; i < list.length; i++)
size += list[i].length();
System.out.println( filename + " is a directory" );
System.out.println( "Size: " + size + " bytes" );
System.out.println( "Contents of the directory: \n" );
String[] lists = file.list();
for( int i = 0; i < lists.length; i++ )
System.out.println( lists[ i ] + "\n" );
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: objectsPosted on 2009-11-05 at 19:48:29ID: 25756468
something like this:
m.au/java/ how-to-rec ursively- t raverse-a- directory- tree
public static long getSize(File directory)
{
long size = 0;
// Get all files in directory
File[] files = directory.listFiles();
for (File file : files)
{
if (file.isDirectory())
{
// Its a directory so (recursively) traverse it
size += getSize(file);
}
else
{
// its a file
size += file.length();
}
}
return size;
}
Adapted from:
http://helpdesk.objects.co