LoginSignup
1
0

More than 5 years have passed since last update.

Javaでフォルダの大きさを計測する

Last updated at Posted at 2018-04-27

Java8でフォルダの大きさを確認する方法を考えた。
単位はバイトです。

javac foldersize.java
java foldersize フォルダのパス
java;foldersize.java

import java.io.IOException;
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];
        }
        System.out.println("["+targetdir+"]"+fileSize(targetdir)+"B");

    }

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


}

ファイル数が多いと結構時間がかかります。

Reference

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0