import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { /* * 报错信息处理 文件路径前三个字母为A-Z、冒号: 和"\" 代表盘符 随后是由小写字母构成的字符串 代表文件夹名 * 使用单个反斜杠间隔 路径的最后一个反斜杠是文件名 * 去除除文件名意外的全部信息 且保留文件名的最后16个字符 若保留16个字符后 文件名相同 且行号相同 则视为同一个报错 * 相同的报错信息以第一次出现的时间为准 至多输出最后八条记录 * 数据接收 * map存储处理之后相关的falseDoc和错误数量 * 数据处理 * 对falseDoc的位置截取前三位后 字符串为小写字母的文件夹名和文件名 * 将falseDoc截取后的位置字符串以”\\“分隔为字符串数组 数组长度减一即为文件名称 判断长度 取sub后16位 * 根据截取后的错误名 和行号 判断map<falseDoc,num>中是否存在这个错误 +1 * 数据输出 * 输出map存储的数据*/ //获取输入内容 FilePathAndlineCountList Scanner scanner = new Scanner(System.in); List<String> FilePathAndlineCountList = new ArrayList<>(); while (scanner.hasNextLine()) { String input = scanner.nextLine().trim(); if (input.equals("")) { break; } FilePathAndlineCountList.add(input); } Map<WrongFile, Integer> statisticsMap = new LinkedHashMap<>(); //信息处理 并将处理结果放入map中 map<文件对象{文件名,文件行数},数量>文件名和行数相同为同一错误文件 for (int i = 0; i < FilePathAndlineCountList.size(); i++) { String TheFilePathBeforeProcessing1 = FilePathAndlineCountList.get(i); String TheFilePathBeforeProcessing2 = FilePathAndlineCountList.get(i); /*fileNameProcessing(TheFilePathBeforeProcessing);//获取文件名 filelineCountProcessing(TheFilePathBeforeProcessing);//获取错误行数*/ WrongFile wrongFile = new WrongFile(fileNameProcessing( TheFilePathBeforeProcessing1), filelineCountProcessing(TheFilePathBeforeProcessing2)); statisticsMap.put(wrongFile, statisticsMap.getOrDefault(wrongFile, 0) + 1); } statisticsMap.entrySet().stream() .skip(Math.max(0, statisticsMap.size() - 8)) .forEach(entry -> System.out.println( entry.getKey().fileName + " " + entry.getKey().lineCount + " " + entry.getValue())); } private static int filelineCountProcessing(String theFilePathBeforeProcessing) { theFilePathBeforeProcessing = theFilePathBeforeProcessing.substring(3, theFilePathBeforeProcessing.length()); int lineCount = Integer.parseInt(theFilePathBeforeProcessing.split(" ")[1]); return lineCount; } private static String fileNameProcessing(String theFilePathBeforeProcessing) { //前三位截取 A-Z : \ 后按照” “分隔 [0]为路径和文件名 [1]为行数 theFilePathBeforeProcessing = theFilePathBeforeProcessing.substring(3, theFilePathBeforeProcessing.length()); String filePath = theFilePathBeforeProcessing.split(" ")[0]; String[] fileNameArray = filePath.split("\\\\"); String fileName = fileNameArray[fileNameArray.length - 1]; if (fileName.length() > 16) { int num = fileName.length() - 16; fileName = fileName.substring(num, fileName.length()); return fileName; } else { return fileName; } } } class WrongFile { String fileName; Integer lineCount; public WrongFile(String fileName, Integer lineCount) { this.fileName = fileName; this.lineCount = lineCount; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; WrongFile that = (WrongFile) o; return Objects.equals(fileName, that.fileName) && Objects.equals(lineCount, that.lineCount); } @Override public int hashCode() { return Objects.hash(fileName, lineCount); } }