OSDN Git Service

43c02aecf918e9f87428515ab13e555b0fec326a
[hayashilib/hayashi.git] / src / jp / co / areaweb / tools / command / DirSize.java
1 package jp.co.areaweb.tools.command;\r
2 import java.io.*;\r
3 \r
4 /**\r
5  * ディレクトリのサイズを求める\r
6  *\r
7  * @author kls040\r
8  * @version 3.0\r
9  */\r
10 public class DirSize {\r
11     /**\r
12     * @param args the command line arguments\r
13     */\r
14     public static void main (String args[]) {\r
15         String outputfile;\r
16         \r
17         if (args.length < 1) {\r
18             outputfile = ".";\r
19         }\r
20         else {\r
21             outputfile = args[0];\r
22         }\r
23 \r
24         try {\r
25             System.out.println(DirSize.size(new File(outputfile)) +"\t"+ outputfile);\r
26         }\r
27         catch(Exception e) {\r
28             e.printStackTrace();\r
29             System.out.println(e.toString());\r
30         }\r
31     }\r
32     \r
33     static long size(File file) throws IOException {\r
34         long size = 0L;\r
35         if (file == null) {\r
36             System.out.println("ERR: ディレクトリが見つかりませんでした。");\r
37             return size;\r
38         }\r
39         \r
40         if (file.isDirectory()) {\r
41             File files[] = file.listFiles();\r
42             if (files != null) {\r
43                 for (int i=0; i < files.length; i++) {\r
44                     size += size(files[i]);\r
45                 }\r
46             }\r
47         }\r
48         else {\r
49             size = file.length();\r
50         }\r
51         return size;\r
52     }\r
53 \r
54 }\r