2012-12-07 25 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