?
Solved

How to get the size of file or directory

Posted on 2007-04-01
6
Medium Priority
?
5,288 Views
Last Modified: 2013-12-14
How 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
0
Comment
Question by:ericylr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1500 total points
ID: 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;
  }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832395
Traverse a directory using: http://www.exampledepot.com/egs/java.io/TraverseTree.html

In the method, keep adding the length :)
0
 

Author Comment

by:ericylr
ID: 18834040
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.getName() + " " + size);
                }else{
                        long filesize = dir.length();
                  System.out.println(dir.getName() + " " + filesize);
                }         
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ericylr
ID: 18836592
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.getName() + " " + size);
                }else{
                        long filesize = dir.length();
                  System.out.println(dir.getName() + " " + filesize);
                }                       
               return info;         
      }


 }
0
 

Author Comment

by:ericylr
ID: 18843836
anybody can help?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18866130
Why are you returning an Object info? It is not getting changed anywhere. use the code that Ajay posted - it uses recursion.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question