Last active
January 7, 2022 08:16
-
-
Save Vyom-Yadav/76cedd6a5286496e25cf762b31ac2e10 to your computer and use it in GitHub Desktop.
Number of lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anotherPackage; | |
import java.io.BufferedInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Test { | |
public static int countLines(File filename) throws IOException { | |
InputStream is = new BufferedInputStream(new FileInputStream(filename)); | |
try { | |
byte[] c = new byte[1024]; | |
int readChars = is.read(c); | |
if (readChars == -1) { | |
// bail out if nothing to read | |
return 0; | |
} | |
// make it easy for the optimizer to tune this loop | |
int count = 0; | |
while (readChars == 1024) { | |
for (int i = 0; i < 1024; ) { | |
if (c[i++] == '\n') { | |
++count; | |
} | |
} | |
readChars = is.read(c); | |
} | |
// count remaining characters | |
while (readChars != -1) { | |
for (int i = 0; i < readChars; ++i) { | |
if (c[i] == '\n') { | |
++count; | |
} | |
} | |
readChars = is.read(c); | |
} | |
return count == 0 ? 1 : count; | |
} | |
finally { | |
is.close(); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
List<Path> resources = Files.walk( | |
Paths.get("/home/vyom/IdeaProjects/checkstyle/src/test/resources")) | |
.filter(Files::isRegularFile) | |
.filter(path -> path.toFile().getName().matches("Input.*\\.java")) | |
.collect(Collectors.toList()); | |
List<Path> resourcesNonCompilable = Files.walk( | |
Paths.get("/home/vyom/IdeaProjects/checkstyle/src/test/resources-noncompilable")) | |
.filter(Files::isRegularFile) | |
.filter(path -> path.toFile().getName().matches("Input.*\\.java")) | |
.collect(Collectors.toList()); | |
resources.addAll(resourcesNonCompilable); | |
resources.forEach(path -> System.out.println(path.toFile().getName())); | |
int numOfLines = 0; | |
List<Integer> sizeOfFile = new ArrayList<>(); | |
for (Path path : resources) { | |
int numOfLinesInFile = countLines(path.toFile()); | |
numOfLines += numOfLinesInFile; | |
sizeOfFile.add(numOfLinesInFile); | |
} | |
Collections.sort(sizeOfFile); | |
System.out.println("Total Num Of Lines: " + numOfLines); | |
System.out.println("Total Num Of Files: " + sizeOfFile.size()); | |
double median = ((double) sizeOfFile.get(sizeOfFile.size() / 2) + (double) sizeOfFile.get( | |
(sizeOfFile.size() / 2) - 1)) / 2; | |
System.out.println("Median of number of lines: " + median); | |
System.out.println("The average number of lines: " + (double) numOfLines / sizeOfFile.size()); | |
} | |
} | |
// You can verify with command-line too, use "find . -name "Input*.java" | wc -l" | |
// to verify the number of files. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment