Thursday, June 26, 2008

Java: getting the size of File Directory

    /**  
     * @author UkiDLucas 
     * @return size of file or directory in bytes [ use: long bytesToMb(long bytes) ]  
     **/
    public static long calcFileDirectorySize(File directory)
    {
if (directory.isFile())
    return directory.length();

long size = 0;

for (File file : directory.listFiles())
{
    size += calcFileDirectorySize(file);
}
return size;
    }

    public static double bytesToMb(long bytes)
    {
return (bytes / 1024 / 1024);
    }