Solved

How to get the size of file or directory

Posted on 2007-04-01
6
5,284 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
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 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

Industry Leaders: 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

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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.

726 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