私は今、自分のコンピューターの前にいないので、間違いを許してください~ OpenCSV API Javadocs はかなり簡潔ですが、それほど多くはないようです。行を読み取ると、コンテンツが文字列の配列に解析されます。空の行は空の文字列配列になり[Ljava.lang.String;@13f17c9e
、印刷しようとすると...
次のサンプルファイルを想定します。
1 |
2 |
3 | "The above lines are empty", 12345, "foo"
myCSVReader.readAll() を実行すると、次のようになります
// List<String[]> result = myCSVReader.readAll();
0 : []
1 : []
2 : ["The above lines are empty","12345","foo"]
質問で説明したことを実行するには、ある種の null チェックや文字列比較ではなく、長さをテストします。
List<String> lines = myCSVReader.readAll();
// lets print the output of the first three lines
for (int i=0, i<3, i++) {
String[] lineTokens = lines.get(i);
System.out.println("line:" + (i+1) + "\tlength:" + lineTokens.length);
// print each of the tokens
for (String token : lineTokens) {
System.out.println("\ttoken: " + token);
}
}
// only process the file if lines two or three aren't empty
if (lineTokens.get(1).length > 0 || lineTokens.get(2).length > 0) {
System.out.println("Process this file!");
processFile(lineTokens);
}
else {
System.out.println("Skipping...!");
}
// EXPECTED OUTPUT:
// line:1 length:0
// line:2 length:0
// line:3 length:3
// token: The above lines are empty
// token: 12345
// token: foo
// Process this file!