Did you ever wonder how many lines of java code does your workspace have? Here a little program that can help you find out:
Class 1:
Class 2:
Class 1:
1: package sample;
2: import java.io.File;
3: import java.util.Scanner;
4: public class LinesOfCodeCounter {
5: private long linesOfCode;
6: private int scannedProjects;
7: public void analizeFolder(File file) {
8: //Check if the current file is a file or a folder
9: if (file.isDirectory()) {
10: //If it is a folder get the inner files
11: File[] filesInFolder = file.listFiles();
12: //For each of the inner files check if it is a folder or a file(Recursion)
13: for (File innerFile : filesInFolder) {
14: analizeFolder(innerFile);
15: }
16: } else if (file.isFile()) {
17: //When you find a file that is not a folder, check if it contains java code
18: if (file.getName().endsWith(".java")) {
19: //Scan the file
20: scanFile(file);
21: }
22: }
23: }
24: public void scanFile(File file) {
25: try {
26: Scanner fileScanner = new Scanner(file);
27: //Read each line of code
28: String tempDataHolder = fileScanner.nextLine();
29: //While the file contain lines of code count them
30: while (fileScanner.hasNext()) {
31: linesOfCode++;
32: tempDataHolder = fileScanner.nextLine();
33: }
34: } catch (Exception ex) {
35: ex.printStackTrace();
36: }
37: }
38: public long getLinesOfCode() {
39: return linesOfCode;
40: }
41: }
Class 2:
1: package sample;
2: import java.io.File;
3: public class Main {
4: public static void main(String[] args) {
5: //The workspace root
6: final File workSpaceRoot = new File("D:\\JavaWorkspace");
7: //Check if the root contains files inside its self
8: LinesOfCodeCounter counter = new LinesOfCodeCounter();
9: counter.analizeFolder(workSpaceRoot);
10: System.out.print(counter.getLinesOfCode());
11: }
12: }