2017-08-24 70 views
0

我想從文件夾中讀取所有日誌文件。我現在正在閱讀一個文件。我已將日誌文件的路徑設置爲String dir。並且我列出了其他例外情況,這些例外情況與getExternalServiceException差不多。我如何使用for loopfor each loopD:\\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); 


    } 


} 

回答

1

您可以使用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 
}); 
0

此代碼應該訣竅:

public static void main(String[] args) 
{ 
    File inputFolder = new File("your_directory_here"); 
    for(File inputFile : inputFolder.listFiles()) 
    { 
     // ignore files that aren't log files 
     if(!f.getName().endsWith("log")) 
      continue; 

     // call a separate method to deal with each file 
     processFile(inputFile); 
    } 
} 

private static void processFile(File logFile) 
{ 
    // enter your logic here 
} 
0

您可以使用下面的代碼來獲得一個文件夾中的所有文件

File folder = new File("/Users/you/folder/"); 
File[] listOfFiles = folder.listFiles(); 

for (File file : listOfFiles) { 
if (file.isFile()) { 
    System.out.println(file.getName()); 
} 
} 
+0

這會給我的文件夾中的文件列表。我也想閱讀所有這些文件。如果我使用路徑作爲'File'對象,我如何通過'main'方法中的'dir'路徑。 –

相關問題