<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class ReadCsv {
enum Headers {
SUMMARY(0), ID(1), STATUS(4);
int value;
Headers(int value) {
this.value = value;
}
int getHeader() {
return value;
}
}
public static void main(String[] args) {
try {
Reader in = new FileReader("C:\\XXX\\YYY.csv");
//Custom reader reads the specific column accordingly
//Auto detected header can also be used, but duplicate header names are not acceptable, will throw exception
Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(Headers.class).parse(in);
for (CSVRecord record : records) {
//Will get the first, second, fifth column.
String summary = record.get(Headers.SUMMARY.getHeader());
String id = record.get(Headers.ID.getHeader());
String status = record.get(Headers.STATUS.getHeader());
System.out.println("Summary: " + summary + ", Id: " + id + ", status: " + status);
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}