这几行代码是我初学时候老师出的一道题,记得当时也是写了很久。欢迎指出错误的地方,一起提高学习!!!
public static void count(File file) {
if (file.isDirectory()) {
for (File t : file.listFiles()) {
if (t.isDirectory()) {
cound(t);
} else if (t.getName().endsWith(".java")) {
++javaFile;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(t));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
javaRow += br.lines().count();
BufferedReader brSpace = null;
try {
brSpace = new BufferedReader(new FileReader(t));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
whiteRow += brSpace.lines().filter(e -> e.trim().length() == 0).count();
}
}
}
}
public static void count(String file) {
cound(new File(file));
}
public static void main(String[] args) {
File ff = new File(System.getProperty("user.dir"));
cound(ff);
System.out.println("Java行数:" + javaRow);
System.out.println("空白行" + whiteRow);
System.out.println("Java文件个数" + javaFile);
}
家人们也可以试着写出除去注释行代码所剩下的行数。