<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.4</version>
</dependency>
groovy
implementation 'com.opencsv:opencsv:5.4'
try (CSVReader reader = new CSVReader(new FileReader("data.csv"))) {
String[] line;
while ((line = reader.readNext()) != null) {
for (String data : line) {
System.out.print(data + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
try (CSVWriter writer = new CSVWriter(new FileWriter("data.csv"))) {
writer.writeNext(header);
writer.writeNext(data1);
writer.writeNext(data2);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withQuoteChar('"')
.withEscapeChar('\\')
.withStrictQuotes(true)
.build();
try (CSVReader reader = new CSVReaderBuilder(new FileReader("data.csv")))
.withCSVParser(parser)
.build()) {
} catch (IOException e) {
e.printStackTrace();
}