假设给你一个日志文件,需要找到拥有 Error 关键信息的行,然后输出该行内容和行号。错误日志内容:
29-May-2023 11:06:53.541 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.32 29-May-2023 11:06:53.545 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 20 2018 19:50:35 UTC 29-May-2023 11:06:53.546 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.32.0 29-May-2023 11:06:53.547 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 8.1 29-May-2023 11:06:53.547 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 6.3 29-May-2023 11:06:53.547 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 29-May-2023 11:06:53.547 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: D:\ProgramFilesFree\Java\jdk1.8.0_45\jre 29-May-2023 11:06:53.548 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_45-b15 29-May-2023 11:06:53.548 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 29-May-2023 11:06:53.548 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: D:\server\tomcat-8.5.32 29-May-2023 11:06:53.548 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: D:\server\tomcat-8.5.32 ... 29-May-2023 11:07:30.463 信息 [Finalizing set up] jenkins.install.SetupWizard.init 29-May-2023 11:07:31.011 信息 [Download metadata thread] hudson.util.Retrier.start Calling the listener of the allowed exception 'sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target' at the attempt #1 to do the action check updates server 29-May-2023 11:07:31.026 信息 [Download metadata thread] hudson.util.Retrier.start Attempted the action check updates server for 1 time(s) with no success 29-May-2023 11:07:31.049 严重 [Download metadata thread] hudson.PluginManager.doCheckUpdatesServer Error checking update sites for 1 attempt(s). Last exception was: SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 29-May-2023 11:07:31.095 信息 [Download metadata thread] hudson.model.AsyncPeriodicWork.lambda$doRun$0 Finished Download metadata. 1,384 ms 29-May-2023 11:07:31.096 信息 [Jenkins initialization thread] hudson.WebAppMain$3.run Jenkins is fully up and running
方式一:使用 BufferedReader 类,每次读取一行,通过一个 lineNumber 记录行号,每读取一行,lineNumber 加一,如下:
package com.hxstrive.java_io.example; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class LogErrorSearch { public static void main(String[] args) { String logFilePath = "error.log"; // 替换为实际的日志文件路径 try (BufferedReader reader = new BufferedReader(new FileReader(logFilePath))) { String line; int lineNumber = 0; while ((line = reader.readLine()) != null) { lineNumber++; if (line.contains("Error")) { System.out.println("错误信息在第 " + lineNumber + " 行:"); System.out.println(line); } } } catch (IOException e) { System.err.println("读取日志文件时发生错误:" + e.getMessage()); } } }
运行输出:
错误信息在第 64 行: 29-May-2023 11:07:31.049 严重 [Download metadata thread] hudson.PluginManager.doCheckUpdatesServer Error checking update sites for 1 attempt(s). Last exception was: SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
方式二:使用 LineNumberReader 类来实现,如下:
package com.hxstrive.java_io.example; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; public class LogErrorSearchWithLineNumberReader { public static void main(String[] args) { String logFilePath = "error.log"; // 需替换为实际的日志文件路径 try (LineNumberReader reader = new LineNumberReader(new FileReader(logFilePath))) { String line; while ((line = reader.readLine()) != null) { if (line.contains("Error")) { int lineNumber = reader.getLineNumber(); System.out.println("错误信息在第 " + lineNumber + " 行:"); System.out.println(line); } } } catch (IOException e) { System.err.println("读取日志文件时出现错误:" + e.getMessage()); } } }
运行效果和方式一一致。