import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.function.Function;
-import java.util.stream.Stream;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
public class JavaJsonLineCounter
{
public static void main(String[] args) throws IOException
{
- printLineCount("..", "java");
- printLineCount("..", "json");
+ printLineCount("../..", "java", "bin", "classes", "SWTHelper");
+ printLineCount("../..", "json", "bin", "classes", "SWTHelper");
}
- private static void printLineCount(String path, String filetype) throws IOException
+ private static void printLineCount(String path, String filetype, String... excludedDirectoryNames) throws IOException
{
- long lineCount = Files.walk(Paths.get(path)).filter(Files::isRegularFile).filter(p -> p.toString().endsWith('.' + filetype))
- .flatMap((Function<Path, Stream<String>>) p ->
+ Set<Path> excludedDirectoryPaths = Arrays.stream(excludedDirectoryNames).map(Paths::get).collect(Collectors.toSet());
+ AtomicLong lineCount = new AtomicLong();
+ AtomicLong byteSize = new AtomicLong();
+ AtomicLong fileCount = new AtomicLong();
+ Files.walk(Paths.get(path)).filter(Files::isRegularFile).filter(p -> p.toString().endsWith('.' + filetype))
+ .filter(p -> !StreamSupport.stream(p.spliterator(), false).anyMatch(excludedDirectoryPaths::contains)).forEach(p ->
{
try
{
- return Files.lines(p);
+ lineCount.addAndGet(Files.lines(p).count());
+ byteSize.addAndGet(Files.size(p));
+ fileCount.incrementAndGet();
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
- }).count();
- System.out.println("Total lines in " + filetype + " files: " + lineCount);
+ });
+ System.out.println(filetype + ": " + fileCount + " files; " + lineCount + " lines; " + byteSize + " bytes");
}
}
\ No newline at end of file