Solved

How to get the size of file or directory

Posted on 2007-04-01
6
5,274 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:mayankeagle
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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:mayankeagle
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now