[Java] 纯文本查看 复制代码 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.Scanner;
/**
*
* @author AnCheng
*
*/
public class NumberOfLinesOfCode {
private static long count = 0L;
private static int year = Calendar.getInstance().get(Calendar.YEAR);
private static int day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
public static void main(String[] args) throws IOException {
File file = inputPath();
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
int num = lineCount(file);
System.out.println(file.getName() + " :" + num + "行代码!");
} else {
System.out.println("该文件不是java源代码文件!");
}
} else {
if (file.isDirectory()) {
fileDir(file);
System.out.println("今天一共写了:" + count + "行代码!");
}
}
}
private static File inputPath() {
File file = null;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入工作空间所在盘符路径:");
String line = sc.nextLine();
file = new File(line);
if (file.exists()) {
break;
} else {
System.out.println("\n输入的目录不存在!请重新输入:");
}
}
sc.close();
return file;
}
private static int lineCount(File file) throws IOException {
FileReader fr = new FileReader(file);
int number = 0;
BufferedReader reader = new BufferedReader(fr);
String line = "";
while ((line = reader.readLine()) != null) {
if (line.length() != 0) {
number++;
}
}
reader.close();
return number;
}
private static void fileDir(File file) throws IOException {
try {
File[] files = file.listFiles();
// System.out.println(file.getAbsolutePath());
// System.out.println(files);
if (files != null) {
for (File file2 : files) {
if (file2.isDirectory() && !file2.isHidden()) {
fileDir(file2);
}
if (file2.isFile() && file2.getName().endsWith(".java")) {
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(file2.lastModified());
int year1 = c1.get(Calendar.YEAR);
int day1 = c1.get(Calendar.DAY_OF_YEAR);
if ((year == year1) && (day == day1)) {
int num = lineCount(file2);
count += num;
System.out.println("该源文件有 " + num + " 行代码 : " + file2.getName() + "\t\t路径为:" + file2.getAbsolutePath());
}
}
}
}
} catch (Exception e) {
}
}
}
|