Reading CSV Headers Into a List

1. Overview

In this short tutorial, we’ll explore different ways of reading CSV headers into a list in Java.

First, we’ll learn how to do this using JDK classes. Then, we’ll see how to achieve the same objective using external libraries such as OpenCSV and Apache Commons CSV.

2. Using BufferedReader

The BufferedReader class provides the easiest solution to tackle our challenge. It offers a fast and efficient way to read a CSV file as it reduces the number of IO operations by reading the content chunk by chunk.

So, let’s see it in action:

class CsvHeadersAsListUnitTest {
    private static final String CSV_FILE = "src/test/resources/employees.csv";
    private static final String COMMA_DELIMITER = ",";
    private static final List<String> EXPECTED_HEADERS = List.of("ID", "First name", "Last name", "Salary");
    @Test
    void givenCsvFile_whenUsingBufferedReader_thenGetHeadersAsList() throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(CSV_FILE))) {
            String csvHeadersLine = reader.readLine();
            List<String> headers = Arrays.asList(csvHeadersLine.split(COMMA_DELIMITER));
            assertThat(headers).containsExactlyElementsOf(EXPECTED_HEADERS);
        }
    }
}

As we can see, we use try-with-resources to create a BufferedReader instance. That way, we make sure that the file is closed afterward. Furthermore, we invoke the readLine() method once to extract the first line which denotes the headers. Finally, we use the split() method alongside Arrays#asList to get the headers as a list.

3. Using Scanner

The Scanner class provides another solution to achieve the same outcome. As the name implies, it scans and reads the content of a given file. So, let’s add another test case to see how to use Scanner to read CSV file headers:

@Test
void givenCsvFile_whenUsingScanner_thenGetHeadersAsList() throws IOException {
    try(Scanner scanner = new Scanner(new File(CSV_FILE))) {
        String csvHeadersLine = scanner.nextLine();
        List<String> headers = Arrays.asList(csvHeadersLine.split(COMMA_DELIMITER));
        assertThat(headers).containsExactlyElementsOf(EXPECTED_HEADERS);
    }
}

Similarly, the Scanner class has the nextLine() method that we can use to get the first line of the input file. Here, the first line represents the headers of our CSV file.

4. Using OpenCSV

Alternatively, we can use the OpenCSV library to read the headers of a particular CSV file. Before getting into the nitty-gritty, let’s add its Maven dependency to the pom.xml file:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.9</version>
</dependency>

Typically, OpenCSV comes with a set of ready-to-use classes and methods for reading and parsing CSV files. So, let’s exemplify the use of this library with a practical example:

@Test
void givenCsvFile_whenUsingOpenCSV_thenGetHeadersAsList() throws CsvValidationException, IOException {
    try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE))) {
        List<String> headers = Arrays.asList(csvReader.readNext());
        assertThat(headers).containsExactlyElementsOf(EXPECTED_HEADERS);
    }
}

As we see above, OpenCSV offers the class CSVReader to read a given file’s content. The CSVReader class provides the readNext() method to retrieve the next first line directly as a String array.

5. Using Apache Commons CSV

Another solution is to use the Apache Commons CSV library. As the name suggests, it offers several handy features for creating and reading CSV files.

To start, we need to add the latest version of its dependency to pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.11.0</version>
</dependency>

In short, the CSVParser class of Apache Commons CSV provides the getHeaderNames() method to return a read-only list of header names:

@Test
void givenCsvFile_whenUsingApacheCommonsCsv_thenGetHeadersAsList() throws IOException {
    CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
      .setDelimiter(COMMA_DELIMITER)
      .setHeader()
      .build();
    try (BufferedReader reader = new BufferedReader(new FileReader(CSV_FILE));
        CSVParser parser = CSVParser.parse(reader, csvFormat)) {
        List<String> headers = parser.getHeaderNames();
        assertThat(headers).containsExactlyElementsOf(EXPECTED_HEADERS);
    }
}

Here, we use the CSVParser class to parse the input file according to the format specified. The headers are parsed automatically from the input file with the help of the setHeader() method.

6. Conclusion

In this short article, we explored different solutions for reading CSV file’s headers as a list.

First, we learned how to do this using JDK. Then, we saw how to achieve the same objective using external libraries.

As always, the code used in this article can be found over on GitHub.

       

\"IT電腦補習
立刻註冊及報名電腦補習課程吧!

Find A Teacher Form:
https://docs.google.com/forms/d/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ/viewform?edit_requested=true#responses

Email:
public1989two@gmail.com






www.itsec.hk
www.itsec.vip
www.itseceu.uk

Be the first to comment

Leave a Reply

Your email address will not be published.


*