全新的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();
}
}
我不确定从哪里开始。 有什么建议吗?
答案 0 :(得分: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 :(得分: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();
}