<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.9.0</version> </dependency> try (Reader reader = new FileReader("data.csv"); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT)) { for (CSVRecord csvRecord : csvParser) { String name = csvRecord.get(0); int age = Integer.parseInt(csvRecord.get(1)); } } catch (IOException e) { e.printStackTrace(); } try (Writer writer = new FileWriter("output.csv"); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { csvPrinter.printRecord("Name", "Age"); csvPrinter.printRecord("John Doe", 30); csvPrinter.printRecord("Jane Smith", 25); csvPrinter.flush(); } catch (IOException e) { e.printStackTrace(); } CSVFormat csvFormat = CSVFormat.DEFAULT .withDelimiter(',') .withQuote('"');


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