import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; try (Reader reader = Files.newBufferedReader(Paths.get("example.csv")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT)) { for (CSVRecord csvRecord : csvParser) { String name = csvRecord.get(0); int age = Integer.parseInt(csvRecord.get(1)); String city = csvRecord.get(2); System.out.println("Name: " + name + ", Age: " + age + ", City: " + city); } } catch (IOException e) { e.printStackTrace(); } import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; try (Writer writer = Files.newBufferedWriter(Paths.get("example.csv")); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { csvPrinter.printRecord("John Doe", 30, "New York"); csvPrinter.printRecord("Jane Smith", 25, "London"); csvPrinter.printRecord("Tom Johnson", 35, "Paris"); csvPrinter.flush(); } catch (IOException e) { e.printStackTrace(); } CSVFormat csvFormat = CSVFormat.newFormat(';').withQuote('"').withHeader("Name", "Age", "City");


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