gpt4 book ai didi

Java程序计算文件中的重复行数

转载 作者:行者123 更新时间:2023-12-02 10:38:51 25 4
gpt4 key购买 nike

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class work {

public static void main(String[] args) throws FileNotFoundException, IOException {
Map m1 = new HashMap();
try (BufferedReader br = new BufferedReader(new FileReader("error.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String[] words = line.split(" ");//**This is where i was strucked**
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {

int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));

newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println("Error : " + key + "Repeated " + m1.get(key) + " times.");
}
}

}
<小时/>

我有一个如下所示的文本文件,我想计算重复行的数量。我对如何拆分此文件并进行计数感到震惊。任何人都可以帮助我吗?

ERROR  [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
2018-10-29T12:01:00Z E! Error in plugin [inputs.openldap]: LDAP Result Code 32 "No Such Object":
ERROR [CompactionExecutor:21454] 2018-10-29 12:02:41,906 NoSpamLogger.java:91 - Maximum memory usage reached (125.000MiB), cannot allocate chunk of 1.000MiB
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml
2018-09-20 14:08:14.571 [main] ERROR org.apache.flink.yarn.YarnApplicationMasterRunner - -Dlogback.configurationFile=file:logback.xml

最佳答案

如果您使用的是 Java8,并且目的是计算重复行(而不是单词),请尝试此操作

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Work {
public static void main(String[] args) throws IOException {
Map<String, Long> dupes = Files.lines(Paths.get("/tmp/error.txt"))
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));

// pretty print
dupes.forEach((k, v)-> System.out.printf("(%d) times : %s ....%n",
v, k.substring(0, Math.min(50, k.length()))));
}
}

输出:

(2) times : 2018-09-20 14:08:14.571 [main] ERROR  org.apache.f ....
(8) times : 2018-10-29T12:01:00Z E! Error in plugin [input ....
(9) times : ERROR [CompactionExecutor:21454] 2018-10-29 12:02 ....
(5) times : 2018-09-20 14:08:14.571 [main] ERROR org.apac ....

关于Java程序计算文件中的重复行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53078079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com