Do you know how can I get the folder size in Java?
The length() method in the File class only works for files, using that method I always get a size of 0.
import java.io.File;
public class GetFolderSize {
int totalFolder = 0;
int totalFile = 0;
public static void main(String args[]) {
String folder = "C:/GetExamples";
try {
GetFolderSize size = new GetFolderSize();
long fileSizeByte = size.getFileSize(new File(folder));
System.out.println("Folder Size: " + fileSizeByte + " Bytes");
System.out.println("Total Number of Folders: "
+ size.getTotalFolder());
System.out.println("Total Number of Files: " + size.getTotalFile());
} catch (Exception e) {}
}
public long getFileSize(File folder) {
totalFolder++;
System.out.println("Folder: " + folder.getName());
long foldersize = 0;
File[] filelist = folder.listFiles();
for (int i = 0; i < filelist.length; i++) {
if (filelist[i].isDirectory()) {
foldersize += getFileSize(filelist[i]);
} else {
totalFile++;
foldersize += filelist[i].length();
}
}
return foldersize;
}
public int getTotalFolder() {
return totalFolder;
}
public int getTotalFile() {
return totalFile;
}
}
There is a slight error with simply recursively iterating over all subfolders. It is possible on some file systems to create circular directory structures using symbolic links as is demonstrated below:
mkdir -- parents father/son
ln -sf ${PWD}/father father/son
ls father/son/father/son/father/son/father/son/
To guard against this error, you can use the java.io.File#getCanonicalPath method. The code below is a slight modification of a previous answer.
public static long getFileSize(File folder) throws IOException {
return ( getFileSize ( folder , new HashSet < String > ( ) ) ) ;
}
public static long getFileSize(File folder, Set<String> history)
throws IOException {
long foldersize = 0;
File[] filelist = folder.listFiles();
for (int i = 0; i < filelist.length; i++) {
System.err.println("HISTORY");
System.err.println(history);
boolean inHistory = history.contains(filelist[i].getCanonicalPath());
history.add(filelist[i].getCanonicalPath());
if (inHistory) {
// skip it
} else if (filelist[i].isDirectory()) {
foldersize += getFileSize(filelist[i], history);
} else {
foldersize += filelist[i].length();
}
}
return foldersize;
}
Go through a folder, file by file, getting the size of each file and adding them to the variable size.
static int size=0;
public static int folderSize(File folder){
size = 0;
File[] fileList = folder.listFiles();
for(File file : fileList){
if(!file.isFile()){
folderSize(file);
}
size += file.length();
}
return size;
}
public class DirectorySize {
public static void main(String[] args) {
// Prompt the user to enter a directory or a file
System.out.print("Please Enter a Directory or a File: ");
Scanner input = new Scanner(System.in);
String directory = input.nextLine();
// Display the size
System.out.println(getSize(new File(directory)) + " bytes");
}
public static long getSize(File file) {
long size = 0; // Store the total size of all files
if (file.isDirectory()) {
File[] files = file.listFiles(); // All files and subdirectories
for (int i = 0; i < files.length; i++) {
size += getSize(files[i]); // Recursive call
}
}
else { // Base case
size += file.length();
}
return size;
}
}
import java.io.File;
import org.apache.commons.io.FileUtils;
public class FolderSize
{
public static void main(String[] args)
{
long size = FileUtils.sizeOfDirectory(new File("C:/Windows/folder"));
System.out.println("Folder Size: " + size + " bytes");
}
}
Iterate over all subfolders in the folder and get the summary size of all files there.