I was working on my little game engine and I was a bit curious… So I made this simple script to see how many lines of code (in total) I had and also how much characters (in total):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Count {
public static void main(String[] args) {
System.out.println(getLineCount(new File("src")) + " lines of code.");
System.out.println(getCharCount(new File("src")) + " characters.");
}
public static int getLineCount(File file) {
if (file.isDirectory()) {
int count = 0;
for (File child : file.listFiles()) {
count += getLineCount(child);
}
return count;
}
int count = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while (reader.readLine() != null) {
count++;
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return count;
}
public static int getCharCount(File file) {
if (file.isDirectory()) {
int count = 0;
for (File child : file.listFiles()) {
count += getCharCount(child);
}
return count;
}
int count = 0;
try {
FileReader reader = new FileReader(file);
while (reader.read() != -1) {
count++;
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return count;
}
}
For me:
Project: Red Game 2D Engine
8176 lines of code.
228337 characters.
About 1000 lines where generated (but I typed the code to generate it).
And I was wondering how many lines/chars other people’s projects had… So give it a try…