2012-12-07 27 views
-3

,我有以下出從文本文件提出:如何解析這個輸出文件在Java中

======================== =======================

guideMenuSuite.mts: 
[monkeytalk:run] MonkeyTalk v1.0.11.beta5c_473 - 2012-07-18 11:38:51 MDT - Copyright 2012 Gorilla Logic, Inc. - www.gorillalogic.com 
[monkeytalk:run] running suite guideMenuSuite.mts... 
[monkeytalk:run] BCOKAS127271: -start suite (1 test) 
[monkeytalk:run] BCOKAS127271: 1 : guide_menu.mt 
[monkeytalk:run] BCOKAS127271: -> OK 
[monkeytalk:run] BCOKAS127271: -end suite 
[monkeytalk:run] result: OK 
[monkeytalk:run] ...done 

==================== ============================

在Java中,我需要: 1)解析輸出設備序列號碼(在這種情況下爲BCOKAS127271 ....它根據正在測試的設備而變化)。 2)獲取在 - >( - > OK)之後的測試的狀態結果。

我試着用拆分,但該數據保持現身空...

任何建議,將不勝感激。

這裏是我的代碼:

CODE

while ((strLine = br.readLine()) != null) 
       { 
        maintextarea.append(strLine + "\n"); 
        System.out.println(strLine); 
        System.out.flush(); 
        maintextarea.append(selectedSerial); 
        delims = "[ \\->]+"; 
        //String[] tokens = strLine.split(delims); 
        //String[] tokens = strLine.substring(prefix.length()).split(delims); 

        String noprefixStr = strLine.substring(strLine.indexOf(prefix) + prefix.length()); 
        String[] tokens = noprefixStr.split(delims); 

        //for (String t: tokens) 
        { 
         //System.out.println(t); 

          if (tokens.toString().contains("ERROR")) 
          { 
           testStatus = "ERROR"; 
           maintextarea.append("\n" + selectedValue + " " + selectedSerial + " = " + testStatus + "\n"); 
           System.out.println(selectedValue + " " + selectedSerial + " = " + testStatus); 
           System.out.flush(); 
          } 

          else if (tokens.toString().contains("FAILURE")) 
          { 
           testStatus = "FAILED"; 
           maintextarea.append("\n" + selectedValue + " " + selectedSerial + " = " + testStatus + "\n"); 
           System.out.println(selectedValue + " " + selectedSerial + " = " + testStatus); 
           System.out.flush(); 
          } 

          else if (tokens.toString().contains("OK")) 
          { 
           testStatus = "PASSED"; 
           maintextarea.append("\n" + selectedValue + " " + selectedSerial + " = " + testStatus + "\n"); 
           System.out.println(selectedValue + " " + selectedSerial + " = " + testStatus); 
           System.out.flush(); 
          } 

          else 
          { 
           testStatus = "N/A"; 
           maintextarea.append("\n" + selectedValue + " " + selectedSerial + " = " + testStatus + "\n"); 
           System.out.println(selectedValue + " " + selectedSerial + " = " + testStatus); 
           System.out.flush(); 
          } 

        } 


       } 


       br.close(); 

============================= ===============================

UPDATE:

我發現基於所有輸入我的解決辦法我從這個團隊收到的! 我最終使它非常簡單,並解析了表達式的輸出文件,然後基於該設置變量來通過或失敗或錯誤。

這是基礎的代碼,我用:

try 
     { 
      BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("/home/ironmantis7x/Documents/TWC_test/MT_test_runner_output.txt")))); 
      String strLine; 
      while ((strLine = br.readLine()) != null) 
      { 
       if (strLine.contains("-> OK")) 
       { 
        testStatus = "Pass"; 
        System.out.println("Test Result = " + testStatus); 
       } 

      } 
     } 

     catch (FileNotFoundException ex) 
     { 
      Logger.getLogger(parseFile.class.getName()).log(Level.SEVERE, null, ex); 
     } 

對不起,我花了這麼久纔回到你們說謝謝!

ironmantis7x

+3

告訴我們,您是否嘗試過的代碼。 –

+0

這是標準輸出格式。我的意思是你所有的輸出文件都是這樣。如果是的話,那很容易。 – Smit

+0

@smit - 是的,這是標準輸出格式,我指向一個文本文件... – ironmantis7x

回答

1

您可以使用正則表達式:

import java.util.Pattern 
import java.util.Matcher 

Pattern status = Pattern.compile("\\[.*] \\w+:\\s+-> (\w+).*"); 

... 

你可以學習如何做到這一點here