### Run Basic 'read' Benchmark Example Source: https://github.com/apache/commons-csv/blob/master/BENCHMARK.md An example command to run the 'read' benchmark test, which typically involves using BufferedReader for line counting. This helps in assessing the basic reading performance. ```shell mvn test -Pbenchmark -Dbenchmark=read ``` -------------------------------- ### Install Skife CSV Prerequisite Source: https://github.com/apache/commons-csv/blob/master/BENCHMARK.md Run this shell script to download and install the Skife CSV implementation JAR file into your local Maven repository, as it's not available on Maven Central. ```shell ./benchmark-prereq.sh ``` -------------------------------- ### Handle Byte Order Marks (BOM) with Commons CSV and Commons IO Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Reads CSV files that may start with a Byte Order Mark (BOM) using `BOMInputStream` from Apache Commons IO. This example also configures the CSV parser to read headers. ```java try (Reader reader = new InputStreamReader(BOMInputStream.builder() .setPath(path) .get(), "UTF-8"); CSVParser parser = CSVFormat.EXCEL.builder() .setHeader() .get() .parse(reader)) { for (CSVRecord record : parser) { String string = record.get("ColumnA"); // ... } } ``` -------------------------------- ### Run All Benchmark Tests Source: https://github.com/apache/commons-csv/blob/master/BENCHMARK.md Execute all JMH benchmark tests for Apache Commons CSV using Maven. Ensure you have the benchmark profile activated. ```shell mvn test -Pbenchmark ``` -------------------------------- ### Run Standalone Performance Test Source: https://github.com/apache/commons-csv/blob/master/BENCHMARK.md Execute the standalone performance test for Apache Commons CSV. Note that this test uses simple timing metrics and does not employ the JMH framework. ```shell mvn test -Dtest=PerformanceTest ``` -------------------------------- ### Add Apache Commons CSV Dependency Source: https://github.com/apache/commons-csv/blob/master/README.md Include this XML snippet in your project's pom.xml to add the Apache Commons CSV library as a dependency. ```xml org.apache.commons commons-csv 1.14.1 ``` -------------------------------- ### Print CSV with Specified Headers Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Creates a CSVPrinter that includes specified headers in the output. This is useful for generating CSV files with a clear header row. ```java Appendable out = ...; CSVPrinter printer = CSVFormat.DEFAULT.builder() .setHeader("H1", "H2") .build() .print(out); ``` -------------------------------- ### Run Specific Benchmark Test Source: https://github.com/apache/commons-csv/blob/master/BENCHMARK.md To run a particular JMH benchmark test, specify its name using the -Dbenchmark JVM system property. This allows for focused testing of specific CSV parsing implementations. ```shell mvn test -Pbenchmark -Dbenchmark= ``` -------------------------------- ### Print CSV with JDBC Column Labels as Headers Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Creates a CSVPrinter that uses JDBC ResultSet column labels as headers. This is convenient for exporting data directly from database queries. ```java try (ResultSet resultSet = ...) { CSVPrinter printer = CSVFormat.DEFAULT.builder() .setHeader(resultSet) .build() .print(out); } ``` -------------------------------- ### Create Custom CSV Format with Commons CSV Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Defines a custom CSV format by extending the default format. Allows customization of comment markers, escape characters, surrounding space handling, quote characters, and quote modes. ```java CSVFormat myFormat = CSVFormat.DEFAULT.builder() .setCommentMarker('#') .setEscape('+') .setIgnoreSurroundingSpaces(true) .setQuote('"') .setQuoteMode(QuoteMode.ALL) .get() ``` -------------------------------- ### Create UTF-8 Reader with BOM Handling Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Creates an InputStreamReader that handles Byte Order Marks (BOMs) and reads UTF-8 encoded bytes. Use this when dealing with CSV files that might have a BOM. ```java /** * Creates a reader capable of handling BOMs. * * @param path The path to read. * @return a new InputStreamReader for UTF-8 bytes. * @throws IOException if an I/O error occurs. */ public InputStreamReader newReader(final Path path) throws IOException { return new InputStreamReader(BOMInputStream.builder() .setPath(path) .get(), StandardCharsets.UTF_8); } ``` -------------------------------- ### Auto-Detect CSV Headers from First Record Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Configures CSV parsing to automatically detect header names from the first record and skip that record during iteration. Use this when the CSV file's first line contains the headers. ```java Reader in = new FileReader("path/to/file.csv"); Iterable records = CSVFormat.RFC4180.builder() .setHeader() .setSkipHeaderRecord(true) .build() .parse(in); for (CSVRecord record : records) { String id = record.get("ID"); String customerNo = record.get("CustomerNo"); String name = record.get("Name"); } ``` -------------------------------- ### Access CSV Record Values by Index Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Reads CSV records and accesses column values using their numerical index. No special CSVFormat configuration is needed for this basic access method. ```java Reader in = new FileReader("path/to/file.csv"); Iterable records = CSVFormat.RFC4180.parse(in); for (CSVRecord record : records) { String columnOne = record.get(0); String columnTwo = record.get(1); } ``` -------------------------------- ### Parse Excel CSV File with Commons CSV Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Parses an Excel CSV file using the predefined EXCEL format. Requires importing `FileReader` and `CSVFormat`. Records can be accessed by column name. ```java Reader in = new FileReader("path/to/file.csv"); Iterable records = CSVFormat.EXCEL.parse(in); for (CSVRecord record : records) { String lastName = record.get("Last Name"); String firstName = record.get("First Name"); } ``` -------------------------------- ### Define CSV Headers Using an Enum Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Parses CSV records using an enum to define header names. This approach reduces errors from using string literals and maps enum constants to column values. ```java public enum Headers { ID, CustomerNo, Name } Reader in = new FileReader("path/to/file.csv"); Iterable records = CSVFormat.RFC4180.builder() .setHeader(Headers.class) .build() .parse(in); for (CSVRecord record : records) { String id = record.get(Headers.ID); String customerNo = record.get(Headers.CustomerNo); String name = record.get(Headers.Name); } ``` -------------------------------- ### Define CSV Headers Manually Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Parses CSV records after defining header names manually. This allows accessing column values by their assigned names instead of indices, improving readability. ```java Reader in = new FileReader("path/to/file.csv"); Iterable records = CSVFormat.RFC4180.builder() .setHeader("ID", "CustomerNo", "Name") .build() .parse(in); for (CSVRecord record : records) { String id = record.get("ID"); String customerNo = record.get("CustomerNo"); String name = record.get("Name"); } ``` -------------------------------- ### Export JDBC ResultSet to CSV String Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Exports data from a JDBC ResultSet to a CSV formatted string using CSVPrinter.printRecords. Ensure proper resource management for Connection, Statement, and ResultSet. ```java final StringWriter sw = new StringWriter(); final CSVFormat csvFormat = CSVFormat.DEFAULT; try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "")) { try (Statement stmt = connection.createStatement(); CSVPrinter printer = new CSVPrinter(sw, csvFormat); ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT, BIN_DATA from TEST")) { printer.printRecords(resultSet); } } final String csv = sw.toString(); System.out.println(csv); ``` -------------------------------- ### Limit ResultSet Rows with CSVFormat.Builder Source: https://github.com/apache/commons-csv/blob/master/src/main/javadoc/overview.html Use `CSVFormat.Builder.setMaxRows(long)` to specify the maximum number of rows to process from a ResultSet. This is useful when the ResultSet is obtained from APIs that do not allow direct SQL or JDBC Statement modification for row limiting. ```java CSVFormat csvFormat = CSVFormat.DEFAULT .setMaxRows(5_000) .get(); try (ResultSet resultSet = ...) { csvFormat.printer().printRecords(resultSet); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.