import java.io.File; import java.io.IOException; /** * ディレクトリのサイズを求める * */ public class DirectorySize { /** * @param args the command line arguments */ public static void main (String args[]) { String checkdir; if (args.length < 1) { checkdir= "../DirectorySize"; } else { checkdir= args[0]; } try { System.out.println(size(new File(checkdir)) +"\t"+ checkdir); } catch(Exception e) { e.printStackTrace(); System.out.println(e.toString()); } } static long size(File file) throws IOException { long size = 0L; if (file == null) { System.out.println("ERR: ディレクトリが見つかりませんでした。"); return size; } if (file.isDirectory()) { File files[] = file.listFiles(); if (files != null) { for (int i=0; i < files.length; i++) { size += size(files[i]); } } } else { size = file.length(); } return size; } }