gpt4 book ai didi

java - 从文件夹中读取所有日志文件

转载 作者:行者123 更新时间:2023-12-02 12:19:42 24 4
gpt4 key购买 nike

我想读取文件夹中的所有日志文件。我现在只读取一个文件。我已将日志文件的路径设置为String dir。我有其他异常的列表,这些异常或多或少与 getExternalServiceException 执行相同的操作。如何使用 D:\\logs 读取所有日志文件for 循环每个循环

警报 - 我需要使用 Java v1.6

public class CallingCheck 
{


public void getExternalServiceException(String sPath) {
BufferedReader br = null;
try {
File f = new File(sPath);
if(f.isFile())
{

FileInputStream fis = new FileInputStream(sPath);
DataInputStream dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
String strLine;

String sPattern = "at (.*)\\.(.*)\\(([^:]*):?([\\d]*)\\)";

Pattern p = Pattern.compile(sPattern);

boolean bFlag = false;
int iCount = 0;
int totCount = 0;
int exCount = 0;


while ((strLine = br.readLine()) != null) {

if((strLine.contains("com.shop.integration.exception.ExternalServiceException")))
//
{ exCount++;
bFlag = true;

}
if(bFlag){

if((strLine.contains("** Error"))){


Matcher m = p.matcher(strLine);
if(m.find()){
totCount++;
iCount++;

if(iCount==1){
System.out.println("Class name:- " + m.group(3));
System.out.println("Line Number:- " + m.group(4));
System.out.println("ExternalServiceException occurence count: " + exCount);

System.out.println("ExternalServiceException stack trace count: " + totCount);

}
if(strLine.contains("at")){
String sTemp[] = strLine.split("\\s+at+\\s+");

strLine = sTemp[1];
strLine = "at "+strLine;
}
System.out.println(strLine);


if(iCount == 5){
bFlag = false;
iCount=0;
}

}
}

}
}}}
catch (Exception e) {
System.out.println(e.getMessage());
}finally{
try{
if(br != null){
br.close();
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

public static void main(String[] args)
{
String dir= "D:\\logs\\readLogFiles.txt";

CallingCheck check = new CallingCheck();
check.getExternalServiceException(dir);


}


}

最佳答案

您可以使用File.listFiles :

File[] fileList = new File(directory).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return true; // we simply want all the files in this directory
// but you can edit to accept specific files
// only depending on certain conditions
}
});

for (File file : fileList) {
// handle each file - read/write etc
}

使用 lambda (Java 8) 更酷:

File[] fileList = new File(directory).listFiles((dir, name) -> {
return true; // we simply want all the files in this directory
});

关于java - 从文件夹中读取所有日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869223/

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