import au.com.bytecode.opencsv.CSVReader;
public class OpenCSVExample {
public static void main(String[] args) {
try (CSVReader reader = new CSVReader(new FileReader("data.csv"))) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
for (String value : nextLine) {
System.out.print(value + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1. Apache Commons CSV
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
public class CommonsCSVExample {
public static void main(String[] args) {
try (CSVParser parser = CSVParser.parse(new FileReader("data.csv"), CSVFormat.DEFAULT)) {
for (CSVRecord record : parser) {
for (String value : record) {
System.out.print(value + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Super CSV
import org.supercsv.io.CsvBeanReader;
import org.supercsv.prefs.CsvPreference;
public class SuperCSVExample {
public static void main(String[] args) {
try (CsvBeanReader reader = new CsvBeanReader(new FileReader("data.csv"), CsvPreference.STANDARD_PREFERENCE)) {
String[] header = reader.getHeader(true);
MyBean bean;
while ((bean = reader.read(MyBean.class, header)) != null) {
System.out.println(bean);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}