How to read csv file in Java?

Reading CSV Files in Java: A Comprehensive Guide

Understanding CSV Files

Before we dive into how to read CSV files in Java, let’s quickly understand what a CSV file is. A CSV file is a plain text file that stores data in a tabular format, similar to a spreadsheet. Each row of the file represents a data record, and each column represents a field or column of data.

Importing the CSV Java Util Class

To read a CSV file in Java, you need to import the java.util class, which includes the CsvWriter class. Here’s how you can import it:

import java.io.*;
import java.util.*;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

Creating a CSV Reader

Next, you need to create a CSVReader object to read the CSV file. Here’s how you can do it:

CSVReader reader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(System.in)))
.withSkipLines(1) // Skip the first line (header)
.build();

Reading the CSV File

Here’s how you can read the CSV file:

String[] lines = reader.readNext();

This line reads the next line from the CSV file, which is equivalent to reading the first row of the file.

Splitting the CSV Lines into Rows

Here’s how you can split the CSV lines into rows:

int[] rows = new int[lines.length];
for (int i = 0; i < lines.length; i++) {
rows[i] = i + 1;
}

This line creates an array of integers, where each integer represents the row number.

Using the Rows Array

Here’s how you can use the rows array to print the CSV file:

for (int i = 1; i < rows.length; i++) {
System.out.println(lines[i]);
}

This loop prints each line of the CSV file, starting from the second line (index 1).

Extracting Data from the Rows Array

Here’s how you can extract the data from the rows array:

String[] columnNames = lines[0].split(",");
String[] data = new String[rows.length];
for (int i = 0; i < rows.length; i++) {
data[i] = lines[i].split(",");
}

This code splits each line into two parts: the column names and the data. The data is stored in the data array.

Main Method

Here’s the complete code:

import java.io.*;
import java.util.*;

public class CSVReader {
public static void main(String[] args) {
CSVReader reader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(System.in)))
.withSkipLines(1) // Skip the first line (header)
.build();
String[] lines = reader.readNext();
int[] rows = new int[lines.length];
for (int i = 0; i < lines.length; i++) {
rows[i] = i + 1;
}
for (int i = 1; i < rows.length; i++) {
System.out.println(lines[i]);
}
String[] columnNames = lines[0].split(",");
String[] data = new String[rows.length];
for (int i = 0; i < rows.length; i++) {
data[i] = lines[i].split(",");
}
System.out.println(columnNames);
System.out.println(data);
}
}

Tips and Tricks

  • Use the csv package instead of opencsv to read CSV files.
  • Use the RowReader class to read multiple rows at once.
  • Use the CSVReader class to read a CSV file from a file path or from a byte array.
  • Use the lineByLine() method to read a CSV file line by line.
  • Use the Headers() method to read a CSV file and ignore the header row.
  • Use the PrintWriter class to write a CSV file.

Conclusion

Reading CSV files in Java is a straightforward process. By following the steps outlined in this article, you can import a CSV file, read the data, and extract it into a Java array. With practice, you’ll become proficient in reading CSV files in Java and can write your own CSV parsers and generators.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top