Posted by Mustafa Sait Özen in , ,

How to Measure Code Lines in a Project (cursory)

Just change projectFolderPath variable's value


import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineCounter {

private static String projectFolderPath = "/home/anyuser/projectFolder"; //"C:\\projectFolder";

private static int totalLineCount = 0;

public static void main(String[] args) {
File projectFolder = new File(projectFolderPath);
lineCountFromFile(projectFolder);
System.out.println(totalLineCount);

}

public static int lineCountFromFile(File file) {

int linecount = 0;

if (file.isFile() && file.canRead() && !file.isHidden()) {
try {
FileReader fr = new FileReader(file);

LineNumberReader ln = new LineNumberReader(fr);

while (ln.readLine() != null) {
linecount++;
}

ln.close();
fr.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

} else {

File[] files = file.listFiles(new LineCounter.ProjectFileFilter());

for (int i = 0; i < files.length; i++) {
lineCountFromFile(files[i]);
}

}
System.out.println(file.getAbsoluteFile() +"'s line count \t"+ linecount);
totalLineCount += linecount;
return linecount;
}

static class ProjectFileFilter implements FileFilter {

private final String[] okFileExtensions = new String[] { "java",
"jspx", "jspa","xhtml", "xml" };

@Override
public boolean accept(File file) {
for (String extension : okFileExtensions) {
if (file.getName().toLowerCase().endsWith(extension)
|| file.isDirectory()) {
return true;
}
}
return false;
}
}
}


This entry was posted on Çarşamba, Ocak 13, 2010 at Çarşamba, Ocak 13, 2010 and is filed under , , . You can follow any responses to this entry through the comments feed .

0 yorum

Yorum Gönder