KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > LineCounter


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.jetspeed.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 public class LineCounter
27 {
28     int count = 0;
29
30     public static void main( String JavaDoc args[] )
31     {
32         LineCounter lc = new LineCounter();
33         int count = 0;
34         int totalCount = 0;
35         for (int ix = 0; ix < args.length; ix++)
36         {
37             count = lc.run(args[ix]);
38             System.out.println( "Count for path [" + args[ix] + "] = " + count );
39             totalCount += count;
40         }
41         System.out.println( "Total Count = " + totalCount );
42     }
43
44     public int run(String JavaDoc path)
45     {
46         System.out.println("Running LineCounter for " + path );
47         count = 0;
48         return traverse(path);
49     }
50
51     private int traverse(String JavaDoc path)
52     {
53         File JavaDoc file = new File JavaDoc(path);
54         if (file.isFile())
55         {
56             try
57             {
58                 String JavaDoc name = file.getName();
59                 if (name.endsWith("java"))
60                     count += countFile(file);
61             }
62             catch (Exception JavaDoc e)
63             {
64                 System.err.println("Failed to count file: " + path + " : " + e.toString());
65             }
66         }
67         else if (file.isDirectory())
68         {
69             if (!path.endsWith(File.separator))
70                 path += File.separator;
71
72     // System.out.println("Processing directory: " + path);
73
String JavaDoc list[] = file.list();
74
75             // Process all files recursivly
76
for(int ix = 0; list != null && ix < list.length; ix++)
77                 traverse(path + list[ix]);
78
79
80         }
81         return count;
82     }
83
84     private int countFile( File JavaDoc file )
85             throws FileNotFoundException JavaDoc, IOException JavaDoc
86     {
87   // System.out.println("Processing file: " + file.getPath());
88
FileReader JavaDoc reader = new FileReader JavaDoc( file );
89         BufferedReader JavaDoc br = new BufferedReader JavaDoc( reader );
90         String JavaDoc s;
91         int mycount = 0;
92
93         while ((s = br.readLine()) != null)
94         {
95             if (s.length() > 0)
96                 mycount++;
97         }
98         return mycount;
99     }
100 }
101
102
Popular Tags