Solved

How to get the size of file or directory

Posted on 2007-04-01
6
5,276 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse debug jspwriter output 3 144
passing a variable to eclipse to debug 2 65
Copying WordPress Pages 5 76
Change local server setting in php 6 81
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: …
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

930 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

9 Experts available now in Live!

Get 1:1 Help Now