Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

10.7. Obtaining the Size of a Directory

10.7.1. Problem

You need to know the size of everything contained within a directory.

10.7.2. Solution

Use the sizeOfDirectory( ) method on FileUtils. The following example returns the size of the temp directory:

File dir = new File( "temp" );
long dirSize = FileUtils.sizeOfDirectory( );

If temp contains a number files, FileUtils.sizeOfDirectory( ) will return the sum of the size of every file in temp. If temp contains subdirectories, this method will recursively call sizeOfDirectory( ) on each subdirectory to obtain the size of each subdirectory, and it will return the sum of these sizes.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.