4

I am designing a small app to take and maintain monthly backups. Here I want to get the folder's size just to verify whether enough space is available on the destination disk or not. I am traversing the directory structure once to take the backup(copy the same dir structure.)Now for finding the size, as I have Mentioned, I don't want to use recursion. So is there any other simpler method?


Thanks to all of you,i have finally used the org.apache.commons.io.FileUtils to get the job done, after all this package also uses recursion.

2
  • 1
    @little bunny foo foo:yeah,my backup code itself is a recursive one,i was just asking if there is another possibility. Commented Jun 26, 2011 at 2:32
  • you should mark the answer that helped you most as accepted - ryan's answer looks like the front runner. Commented Jun 27, 2011 at 10:20

4 Answers 4

4

Not sure if that is possible.

Even FileUtils.sizeOfDirectory() in apache-commons IO uses recursion to get the directory size.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, I think this is the only option, otherwise explicit recursion is required.
It's entirely possible to avoid recursion by using a suitable supporting data structure, but it doesn't really help because you still have to do a directory tree structure traversal. Recursion's good because it is simple to get right.
1

I believe this is not possible. Even operating systems have to recurse through directory structures to determine a directory's size. As far as I know, no modern file system stores the size of all of the contained files.

Comments

0

Java functionality like this is implemented as part of the Java API. Nothing there provides what you want. Apache Commons offers the library FileUtils, which has sizeOfDirectory. Recursion is certainly used in this. Why don't you want to use recursion?

2 Comments

As i have mentioned i have recursive code that copies the dir struct. now if i call one more method that is also recursive,then its going to increase the run time as i am traversing the same thing recursively twice.
@buch, First, it probably doesn't significantly matter speed-wise that you traverse it a second time; the slow part is going to be the disk IO which is only present in the first round. That being said, why don't you combine the two? Recursion is necessary here.
0

Why don't you determine the directory size during the first recursive pass?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.