使用try-with-resources处理Stream

This commit is contained in:
fengyarnom 2024-11-06 17:08:41 +08:00
parent 3537aaa7e7
commit 9ed198a360

View File

@ -8,6 +8,7 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WarningLogExecutor {
@ -32,14 +33,15 @@ public class WarningLogExecutor {
initializeWarningFile(warningFile, keyword, warningTypeName);
}
// 找到日志文件
List<Path> logFiles = Files.list(Paths.get(logDirectory))
try (Stream<Path> logFilesStream = Files.list(Paths.get(logDirectory))) {
List<Path> logFiles = logFilesStream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
.collect(Collectors.toList());
for (Path logFile : logFiles) {
executor.submit(() -> processFile(logFile, keyword, warningTypeName, warningFile));
executor.submit(() -> processFile(logFile, keyword, warningFile));
}
}
} catch (IOException e) {
e.printStackTrace();
@ -67,7 +69,7 @@ public class WarningLogExecutor {
}
}
private void processFile(Path logFile, String keyword, String warningTypeName, Path warningFile) {
private void processFile(Path logFile, String keyword, Path warningFile) {
try (BufferedReader reader = Files.newBufferedReader(logFile);
BufferedWriter writer = Files.newBufferedWriter(warningFile,
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {