import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; public class CSVReaderExample { public static void main(String[] args) { try { CSVParser parser = CSVParser.parse(new File("example.csv"), Charset.defaultCharset(), CSVFormat.DEFAULT); for (CSVRecord record : parser) { String name = record.get(0); int age = Integer.parseInt(record.get(1)); String city = record.get(2); System.out.println("Name: " + name + ", Age: " + age + ", City: " + city); } parser.close(); } catch (IOException e) { e.printStackTrace(); } } } import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class CSVWriterExample { public static void main(String[] args) { try { CSVPrinter printer = new CSVPrinter(new FileWriter("example.csv"), CSVFormat.DEFAULT); printer.printRecord("John Doe", 25, "New York"); printer.printRecord("Jane Smith", 30, "London"); printer.close(); } catch (IOException e) { e.printStackTrace(); } } } CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';').withHeader("Name", "Age", "City"); CSVParser parser = CSVParser.parse(new File("example.csv"), Charset.defaultCharset(), format);


上一篇:
下一篇:
切换中文