How to Read CSV File in Java: Simple Guide with Example
To read a CSV file in Java, use
BufferedReader to read the file line by line and split each line by commas using String.split(","). This simple approach lets you process each row as an array of values.Syntax
Use BufferedReader to open and read the file line by line. For each line, use String.split(",") to separate values by commas into an array.
Example parts:
BufferedReader br = new BufferedReader(new FileReader("file.csv"));opens the file.String line = br.readLine();reads one line.line.split(",")splits the line into values.br.close();closes the file.
java
BufferedReader br = new BufferedReader(new FileReader("file.csv")); String line; while ((line = br.readLine()) != null) { String[] values = line.split(","); // process values } br.close();
Example
This example reads a CSV file named data.csv and prints each value separated by a dash.
java
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadCSV { public static void main(String[] args) { String filePath = "data.csv"; try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String line; while ((line = br.readLine()) != null) { String[] values = line.split(","); for (int i = 0; i < values.length; i++) { System.out.print(values[i]); if (i < values.length - 1) { System.out.print(" - "); } } System.out.println(); } } catch (IOException e) { System.out.println("Error reading file: " + e.getMessage()); } } }
Output
John - Doe - 30
Jane - Smith - 25
Bob - Johnson - 40
Common Pitfalls
Common mistakes when reading CSV files include:
- Not closing the
BufferedReader, which can cause resource leaks. - Assuming all lines have the same number of values.
- Not handling commas inside quoted values (this simple method does not support that).
- Not handling exceptions properly.
For complex CSV files with quoted commas, consider using libraries like OpenCSV.
java
/* Wrong way: Not closing BufferedReader and ignoring exceptions */ BufferedReader br = new BufferedReader(new FileReader("file.csv")); String line = br.readLine(); while (line != null) { String[] values = line.split(","); // process values line = br.readLine(); } /* Right way: Using try-with-resources to auto-close and handle exceptions */ try (BufferedReader br = new BufferedReader(new FileReader("file.csv"))) { String line; while ((line = br.readLine()) != null) { String[] values = line.split(","); // process values } } catch (IOException e) { e.printStackTrace(); }
Quick Reference
| Step | Description | Code snippet |
|---|---|---|
| Open file | Create BufferedReader to read file | BufferedReader br = new BufferedReader(new FileReader("file.csv")); |
| Read line | Read one line at a time | String line = br.readLine(); |
| Split line | Split line by commas into array | String[] values = line.split(","); |
| Process data | Use values array as needed | // process values |
| Close file | Close BufferedReader to free resources | br.close(); or try-with-resources |
Key Takeaways
Use BufferedReader with FileReader to read CSV files line by line.
Split each line by commas using String.split(",") to get individual values.
Always close the BufferedReader to avoid resource leaks, preferably with try-with-resources.
This method works for simple CSV files without quoted commas or special characters.
For complex CSV parsing, use dedicated libraries like OpenCSV.