1. Introduction

Reading data from files is a common task in many applications. When working with structured data such as CSV files, we can store the contents in a 2D array for easier access and manipulation.

In this tutorial, we’ll cover three approaches for reading a file into a 2D array in Java: a BufferedReader, the Java 8 Nonblocking IO (NIO) API, and the Apache Commons CSV library.

2. Problem Statement

A common way to represent data from external files, such as CSV files, is through a 2D array, where each row corresponds to a line in the file, and each column contains an individual data value.

For example, if the file contains the following content:

value1,value2,value3
value4,value5,value6
value7,value8,value9

We can load this data into a 2D array like:

String[][] expectedData = {
    {"value1", "value2", "value3"},
    {"value4", "value5", "value6"},
    {"value7", "value8", "value9"}
};

The challenge lies in choosing the right approach to efficiently read and process the file while ensuring flexibility for larger or more complex datasets.

3. Using BufferedReader

The most straightforward approach to reading a file into a 2D array is with a BufferedReader:

try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
    List<String[]> dataList = new ArrayList<>();
    String line;
    while ((line = br.readLine()) != null) {
        dataList.add(line.split(","));
    }
    return dataList.toArray(new String[0][]);
}

First, we create a BufferedReader with a FileReader, which allows us to read each line from the file efficiently. We’ll use an ArrayList to store the data dynamically.

Additionally, the try-with-resources statement ensures that the BufferedReader is automatically closed after reading the file, preventing resource leaks. The dynamically sized ArrayList, called dataList, stores each file row as a string array.

Inside the loop, br.readLine() reads each line from the file individually until all lines have been read, after which readLine() returns null. We then split() each line into an array of strings. Finally, we convert the ArrayList into a 2D array using the toArray() method.

Furthermore, let’s test this implementation by comparing the actual data read from the file with the expected 2D array:

@Test
public void givenFile_whenUsingBufferedReader_thenArrayIsCorrect() throws IOException {
    String[][] actualData = readFileTo2DArrayUsingBufferedReader("src/test/resources/test_file.txt");

    assertArrayEquals(expectedData, actualData);
}

4. Using java.nio.file.Files

In Java 8, we can leverage the java.nio.file.Files class for a more concise approach to reading a file into a 2D array. Instead of manually reading each line with a BufferedReader, we can use the readAllLines() method to read all lines from the file at once:

List<String> lines = Files.readAllLines(Paths.get(filePath));
return lines.stream()
        .map(line -> line.split(","))
        .toArray(String[][]::new);

Here, we read all lines from the file and store them in a List. Next, we convert the list of lines into a stream using the lines.stream() method. Subsequently, we use map() to transform each line in the stream, where line.split() creates an array of strings. Finally, we use toArray() to collect the result into a 2D array.

Let’s test this approach to ensure it also reads the file into the expected 2D array:

@Test
public void givenFile_whenUsingStreamAPI_thenArrayIsCorrect() throws IOException {
    String[][] actualData = readFileTo2DArrayUsingStreamAPI("src/test/resources/test_file.txt");

    assertArrayEquals(expectedData, actualData);
}

Note that this approach improves readability and reduces the amount of boilerplate code, making it a great option for modern Java applications.

5. Using Apache Commons CSV

For more complex CSV file formats, the Apache Commons CSV library provides a robust and flexible solution. To read the CSV file, we first create a CSVParser instance by reading the file using the CSVFormat class:

Reader reader = new FileReader(filePath);
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);

In this snippet, we also use a FileReader to read the contents of the specified file. The CSVParser is initialized with the reader and configured to use the default CSV format.

Next, we can iterate through the records in the CSV file and store them in a list:

List<String[]> dataList = new ArrayList<>();
for (CSVRecord record : csvParser) {
    String[] data = new String[record.size()];
    for (int i = 0; i < record.size(); i++) {
        data[i] = record.get(i);
    }
    dataList.add(data);
}

return dataList.toArray(new String[dataList.size()][]);

Each CSVRecord is a row, and the values within each record are the columns. By the end, we’ll have a fully populated 2D array with the file’s data.

Let’s test this method using Apache Commons CSV to confirm it works as expected:

@Test
public void givenFile_whenUsingApacheCommonsCSV_thenArrayIsCorrect() throws IOException {
    String[][] actualData = readFileTo2DArrayUsingApacheCommonsCSV("src/test/resources/test_file.csv");

    assertArrayEquals(expectedData, actualData);
}

The Apache Commons CSV library deals with many types of string sanitization, such as quoted fields, different delimiters, and escape characters, making it a great solution for large and complex CSV files.

6. Conclusion

Each method discussed in this tutorial offers a unique way to read a file into a 2D array in Java. The BufferedReader method gives us full control over the reading process, while the Java NIO API provides a more concise and functional solution. The Apache Commons CSV library is an excellent choice for more complex CSV data, offering greater flexibility and robustness.

As always, the complete code samples for this article can be found over on GitHub.