
net.anotheria.util.tools.WC Maven / Gradle / Ivy
package net.anotheria.util.tools;
import net.anotheria.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* Wordcount utility. Walks recursively through a directory and counts all lines/words/chars in all files which matches the extension (for now .java).
*
* @author lrosenberg
* @since 25.06.2004
* @version $Id: $Id
*/
public final class WC {
/**
* Default extension. Files with another extension will be ignored.
*/
private static final String EXT = ".java";
private static final Logger log = LoggerFactory.getLogger(WC.class);
private static int totalLines, totalWords, totalChars;
private static int totalFiles;
/**
* main.
*
* @param a a {@link java.lang.String} object.
*/
public static void main(String... a){
totalLines = totalWords = totalChars = 0;
totalFiles = 0;
if (a.length==0){
a = new String[1];
a[0] = ".";
}
List toCheck = Arrays.asList(a);
new Walker(new WCWorker()).start(toCheck);
System.out.println("Total files:"+totalFiles+" total chars: "+totalChars+", total words: "+totalWords+", total length: "+totalLines);
}
private static class WCWorker implements Worker{
@Override
public void processFile(File file) {
if (!file.getName().endsWith(EXT))
return;
try (FileInputStream fIn = new FileInputStream(file)) {
byte[] d = new byte[fIn.available()];
fIn.read(d);
String s = new String(d);
s = StringUtils.removeChar(s, '\r');
s = StringUtils.removeCComments(s);
s = StringUtils.removeCPPComments(s);
int lines, chars;
int words = lines = chars = 0;
String currentLine = "";
boolean inWord = false;
int i = 0;
while( i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy