我想从文件夹中读取所有日志文件。我现在正在阅读一个文件。我已将日志文件的路径设置为String dir
。并且我列出了其他例外情况,这些例外情况与getExternalServiceException
差不多。我如何使用for loop
或for each loop
从D:\\logs
读取所有日志文件。从文件夹中读取所有日志文件
警报 - 我需要使用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'对象,我如何通过'main'方法中的'dir'路径。 –