public static long sizeOfDirectory(File directory)
Counts the size of a directory recursively (sum of the length of all files). Note that overflow is not detected, and the return value may be negative if overflow occurs. See sizeOfDirectoryAsBigInteger(File) for an alternative method that does not overflow.
Parameters:
directory - directory to inspect, must not be null
Returns:
size of directory in bytes, 0 if directory is security restricted, a negative number when the real total is greater than Long.MAX_VALUE.
Throws:
NullPointerException - if the directory is null
DirectorySize.java
Counts the size of a directory recursively (sum of the length of all files). Note that overflow is not detected, and the return value may be negative if overflow occurs. See sizeOfDirectoryAsBigInteger(File) for an alternative method that does not overflow.
Parameters:
directory - directory to inspect, must not be null
Returns:
size of directory in bytes, 0 if directory is security restricted, a negative number when the real total is greater than Long.MAX_VALUE.
Throws:
NullPointerException - if the directory is null
DirectorySize.java
package com.javatutorialcorner.directory; import java.io.File; import org.apache.commons.io.FileUtils; public class DirectorySize { public static void main(String[] args) { long sizeInBytes = FileUtils.sizeOfDirectory(new File("C:/JTC")); System.out.println("Directory Size in Bytes: " + sizeInBytes + " bytes"); } }Output
Directory Size in Bytes: 240 bytes
0 comments:
Post a Comment