<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0-SNAPSHOT</version>
</dependency>
</dependencies>
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class ReadCSV {
public static void main(String[] args) {
String csvFile = "path/to/your/csv/file.csv";
try (Reader reader = new FileReader(csvFile);
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT)) {
for (CSVRecord csvRecord : csvParser) {
String col1 = csvRecord.get(0);
String col2 = csvRecord.get(1);
System.out.println("Column 1: " + col1);
System.out.println("Column 2: " + col2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriteCSV {
public static void main(String[] args) {
String csvFile = "path/to/your/csv/file.csv";
try (Writer writer = new FileWriter(csvFile);
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
csvPrinter.printRecord("Value 1", "Value 2", "Value 3");
csvPrinter.printRecord("1", "2", "3");
csvPrinter.printRecord("4", "5", "6");
csvPrinter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}