Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get the size of file or directory

Posted on 2007-04-01
6
Medium Priority
?
5,291 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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: …
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.
Suggested Courses

609 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