2014-11-03 59 views
-2

Groovy和Java的新變種。Groovy或Java來解析文本文件並比較

我有一個大的製表符分隔的文本文件。

我需要能夠'測試'文本文件的每一行,以確保某些列有正確的數據。

例如,

column 2 should only have the term 'New Customer' 
column 14 should only have the term 'Dog' or 'Cat' 

如果任何比較失敗,然後打印不正確的術語。

package TestImport 

import java.io.BufferedReader; 
import java.io.FileReader; 

public class PreValidateData { 

    public static void main(String args[]) throws Exception { 

     String dataFileName = "C:/Users/BigDaddy/Desktop/test.csv" 
     BufferedReader bReader = new BufferedReader(new FileReader(dataFileName)); 
     String line; 

     while (line = bReader.readLine()) { 

      //Not sure what to put here. This doesn't work 
      String datavalue[] = line.split("\t"); 
      String value2 = datavalue[1]; 
      String value14 = datavalue[13]; 

      if(value2 != "New Customer"){ 
       Println("FAILURE: line:" + value2.linenumber + "in column 2 is not New Customer. but = " + datavalue[2]) 
      } 

      if(value14 != "Ndog" or "cat){ 
       Println("FAILURE: line:" + value14.linenumber + "in column 14 is not cat or dog. but = " + term) 
      } 

     } 
     bReader.close(); 
    } 
} 

我不確定哪裏可以開始。 有什麼建議嗎?

+2

http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – 2014-11-03 16:59:30

+1

不確定從哪裏開始?如何在Java中使用String比較工作? – Tom 2014-11-03 16:59:48

+1

爲什麼這個問題標籤爲「spock」​​? – 2014-11-03 17:14:06

回答

1

您的語法接近,您可以使用try-with-resources,我會使用格式化的io(printf)。類似的,

try (BufferedReader bReader = new BufferedReader(new FileReader(
     dataFileName))) { 
    String line; 
    int lineNumber = 0; 
    while ((line = bReader.readLine()) != null) { 
     lineNumber++; 
     String datavalue[] = line.split("\t"); 
     String value2 = datavalue[1]; 
     String value14 = datavalue[13]; 

     if (!value2.equals("New Customer")) { 
      System.err.printf("FAILURE: line: %d in column 2 is " 
        + "%s not 'New Customer'.%n", lineNumber, value2); 
     } 
     if (!(value14.equals("dog") || value14.equals("cat"))) { 
      System.err.printf("FAILURE: line: %d in column 14 is " 
        + "%s not 'dog' or 'cat'.%n", lineNumber, value14); 
     } 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

完美。最後一個問題。我如何忽略第一行,因爲它有頭文件? – GreetRufus 2014-11-03 18:01:18

+0

只需在'while循環前添加'bReader.readLine()'。 – Opal 2014-11-03 18:05:01

+0

優秀。非常感謝幫助的人!!!!! – GreetRufus 2014-11-03 20:53:34

3

請使用Groovy來做到這一點。用Java寫這個是浪費擊鍵。

例子:

def file = new File("C:/Users/BigDaddy/Desktop/test.csv") 
def lineCount = 0 
file.eachLine { line -> 
    def parts = line.split '\t' 
    assert parts.size() > 13 
    if (parts[ 1 ] != 'New Customer') 
     System.err.println "Failure! Line $lineCount in column 2..." 
    if (! (parts[ 13 ] in [ 'dog', 'cat' ])) 
     System.err.println "Failure! Line $lineCount in column 14..." 
    lineCount++ 
} 

運行幾乎一樣快,但Java的漂亮得多讀/寫。

+1

如果你想忽略一行並使用某種編碼,只需將這些參數傳遞給'eachLine'方法:'file.eachLine('utf-8',1){...} – Renato 2014-11-03 21:34:23