package springboot_001.main;
/**
* @author bwx686385 bianqiang
* @create 2020/5/6
*/
import org.apache.commons.lang.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
* @author xWX620347
*
*/
public class Test {
public static void main(String[] args) throws Exception {
listFile(new File("D:\\idea_workspace\\ows_csp"));
System.out.println(fileCount);
System.out.println(lineCount);
}
public static Map<String, Integer> fileCount = new LinkedHashMap<>();
public static Map<String, Integer> lineCount = new LinkedHashMap<>();
public static Set<String> fileLineExt = new HashSet() {{
add("java");
add("html");
add("js");
add("css");
}};
public static void listFile(File file) throws Exception {
if (file.isDirectory() && file.listFiles() != null) {
if (".git".equals(file.getName())) {
return;
}
for (File f : file.listFiles()) {
listFile(f);
}
} else {
String name = file.getName();
String ext = null;
if (name.contains(".")) {
ext = name.substring(name.lastIndexOf(".") + 1);
}
if (StringUtils.isEmpty(ext)) {
return;
}
if (ext.length() < 1 || ext.length() > 4) {
return;
}
if (fileLineExt.contains(ext)) {
Integer count = fileCount.get(ext);
if (count == null) {
count = 1;
} else {
count++;
}
fileCount.put(ext, count);
Integer line = lineCount.get(ext);
if (line == null) {
line = 0;
}
line += getFileLinesCount(file);
lineCount.put(ext, line);
}
}
}
public static int getFileLinesCount(File file) throws Exception {
// long fileLength = file.length();
// LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file));
// lineNumberReader.skip(fileLength);
// int lines = lineNumberReader.getLineNumber();
// lineNumberReader.close();
// return lines;
long lines = 0;
try {
lines = Files.lines(Paths.get(file.getPath())).count();
} catch (Exception ex) {
}
return (int) lines;
}
}
https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/combine/chapter3_1.shtml