Measure the size of a folder in Java

I thought about how to check the size of a folder in Java 8. The unit is bytes.

java foldersize folder path


import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class foldersize {
    public static void main(String args[]) {
        String targetdir;
        if (args.length == 0) {
            targetdir = ".";
        } else {
            targetdir = args[0];


    public static long fileSize(String path) {
        Path folder = Paths.get(path);
        long size = 0;
        try {
            size =  Files.walk(folder)
            .filter(f -> f.isFile())
            .mapToLong(f -> f.length())
        } catch (IOException e) {
        return size;


If there are many files, it will take a long time.


Recommended Posts

Measure the size of a folder in Java
Easily measure the size of Java Objects
A quick explanation of the five types of static in Java
Get the result of POST in Java
The story of writing Java in Emacs
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
The story of forgetting to close a file in Java and failing
The story of learning Java in the first programming
Feel the passage of time even in Java
A quick review of Java learned in class
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java
Validate the identity token of a user authenticated with AWS Cognito in Java
Get the URL of the HTTP redirect destination in Java
Count the number of occurrences of a string in Ruby
A quick review of Java learned in class part4
A note for Initializing Fields in the Java tutorial
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
[Java] When writing the source ... A memorandum of understanding ①
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Change the storage quality of JPEG images in Java
A survey of the Kubernetes native Java framework Quarkus
Summarize the additional elements of the Optional class in Java 9
Find a subset in Java
Implementation of gzip in java
Implementation of tri-tree in Java
[Java] Integer information of characters in a text file acquired by the read () method
Was done in the base year of the Java calendar week
Let's make a calculator application in Java ~ Display the application window
Check the dependency of a specific maven artifact in Coursier
[Java] ArrayList → Should I specify the size in array conversion?
Determine that the value is a multiple of 〇 in Ruby
Activate Excel file A1 cell of each sheet in Java
Create a method to return the tax rate in Java
Count the number of digits after the decimal point in Java
A program that counts the number of words in a List
How to derive the last day of the month in Java
I tried to make a client of RESAS-API in Java
Find the number of days in a month with Kotlin
A program (Java) that outputs the sum of odd and even numbers in an array
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
Access the network interface in Java
Think of a Java update strategy
[Java] Delete the elements of List
Guess the character code in Java
3 Implement a simple interpreter in Java
Specify the java location in eclipse.ini
Sort a List of Java objects
Unzip the zip file in Java