Friday 29 July 2022

How to get the directory size in Java?

Write a program to print the folder or directory size in bytes, KBs, MBs and GBs.

 

Approach 1: Traverse the folder recursively and accumulate all the files size.

 


FolderSize.java

package com.sample.app.files;

import java.io.File;

public class FolderSize {

	private static long getFolderSizeInBytes(final File folder) {
		long lengthInBytes = 0;

		final File[] files = folder.listFiles();

		int count = files.length;

		for (int i = 0; i < count; i++) {
			if (files[i].isFile()) {
				lengthInBytes += files[i].length();
			} else {
				lengthInBytes += getFolderSizeInBytes(files[i]);
			}
		}
		return lengthInBytes;
	}

	private static double bytesToKB(long sizeInBytes) {
		return sizeInBytes / (double) 1024;
	}

	private static double bytesToMB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024);
	}

	private static double bytesToGB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024 * 1024);
	}

	public static void main(String[] args) {
		File folder = new File("/Users/krishna/Documents ");
		long folderSizeInBytes = getFolderSizeInBytes(folder);

		System.out.println("Folder size in bytes : " + folderSizeInBytes);
		System.out.println("Folder size in KB : " + bytesToKB(folderSizeInBytes));
		System.out.println("Folder size in MB : " + bytesToMB(folderSizeInBytes));
		System.out.println("Folder size in GB : " + bytesToGB(folderSizeInBytes));
	}

}

 

Approach 2: Using Files.walkFileTree. Files.walkFileTree method walks a file tree.

 

FolderSize.java

 

package com.sample.app.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicLong;

public class FolderSize {

	private static long getFolderSizeInBytes(final File folder) {

		Path directory = folder.toPath();
		final AtomicLong lengthInBytes = new AtomicLong(0);

		try {
			Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					lengthInBytes.addAndGet(attrs.size());
					return FileVisitResult.CONTINUE;
				}
			});
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}

		return lengthInBytes.longValue();
	}

	private static double bytesToKB(long sizeInBytes) {
		return sizeInBytes / (double) 1024;
	}

	private static double bytesToMB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024);
	}

	private static double bytesToGB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024 * 1024);
	}

	public static void main(String[] args) {
		File folder = new File("/Users/krishna/Documents");
		long folderSizeInBytes = getFolderSizeInBytes(folder);

		System.out.println("Folder size in bytes : " + folderSizeInBytes);
		System.out.println("Folder size in KB : " + bytesToKB(folderSizeInBytes));
		System.out.println("Folder size in MB : " + bytesToMB(folderSizeInBytes));
		System.out.println("Folder size in GB : " + bytesToGB(folderSizeInBytes));
	}

}

 

Approach 3: Using Files.walk method.

 

Files.walk method return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by Path#resolve(Path) resolving the relative path against start.

 

FolderSize.java

 

package com.sample.app.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FolderSize {

	private static long getFolderSizeInBytes(final File folder) {

		Path directory = folder.toPath();

		long lengthInBytes = 0l;
		try {
			lengthInBytes = Files.walk(directory).filter(file -> file.toFile().isFile())
					.mapToLong(file -> file.toFile().length()).sum();
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}

		return lengthInBytes;
	}

	private static double bytesToKB(long sizeInBytes) {
		return sizeInBytes / (double) 1024;
	}

	private static double bytesToMB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024);
	}

	private static double bytesToGB(long sizeInBytes) {
		return sizeInBytes / (double) (1024 * 1024 * 1024);
	}

	public static void main(String[] args) {
		File folder = new File("/Users/krishna/Documents");
		long folderSizeInBytes = getFolderSizeInBytes(folder);

		System.out.println("Folder size in bytes : " + folderSizeInBytes);
		System.out.println("Folder size in KB : " + bytesToKB(folderSizeInBytes));
		System.out.println("Folder size in MB : " + bytesToMB(folderSizeInBytes));
		System.out.println("Folder size in GB : " + bytesToGB(folderSizeInBytes));
	}

}

 

You may like

Get the content of resource file as string

Copy the content of file to other location

Write byte array to a file

How to download a binary file in Java?

How to process a huge file line by line in Java?

How to process a large file in chunks?

 


No comments:

Post a Comment