Traverse a directory using: http://www.exampledepot.co
In the method, keep adding the length :)
Main Topics
Browse All TopicsHow to get the size of file or directory, The size of a directory should be the total size of all contained files? i know the method File.length will return the size of a file (but not of a directory).
for example:
one-a 52
one-b 60
one 112
three-a 24
three 24
two 0
test0 136
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.
Traverse a directory using: http://www.exampledepot.co
In the method, keep adding the length :)
Ajay-Singh: thank you very much, below is my code:
if (dir.isDirectory()){
File[] list = dir.listFiles();
long size=0;
for (int i = 0; i < list.length; i++)
size += list[i].length();
System.out.println(dir.get
}else{
long filesize = dir.length();
System.out.println(dir.get
}
My testing structure is Directory: test0; Sub-directory: one,two,three;
C:\>java SizeTest c:\test0
one-a.txt 303
one-b.txt 1087
one-c.txt 0
one 1390
three 0
two-a.txt 1041
two-b.txt 1049
two 1041
test0 0
the output is not correct, the size of Directory Test0 should not be 0
The question reqiure me edit the afterLast Method to print out the directory and file size, how to use recursive in the afterLast method?
Below is my code:
public class SizeList extends DirectoryTour{
public Object afterLast(File dir, Object info) {
if (dir.isDirectory()){
File[] list = dir.listFiles();
long size=0;
for (int i = 0; i < list.length; i++)
size += list[i].length();
System.out.println(dir.get
}else{
long filesize = dir.length();
System.out.println(dir.get
}
return info;
}
}
Business Accounts
Answer for Membership
by: Ajay-SinghPosted on 2007-04-01 at 06:42:03ID: 18831839
You can use this recursive function:
public long size(File file) {
if (file.isFile())
return file.length();
File[] list = file.listFiles();
if (null == list || list.length == 0)
return 0;
long size = 0;
for (int i = 0; i < list.length; i++)
size += size(list[i]);
return size;
}