Parsing CSV file using Java code example (Comma Separated File)


A CSV file is a comma-separated text file with double quotes as escape characters for a comma in a string element. In this tutorial, we will see how we can parse a CSV file using Java code example,

package com.code2care.tutorials;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 
 * Java Code to process CSV file.
 * 
 * @author Code2care Tutorials
 *
 */
public class JavaArrayToArrayList {

	public static void main(String[] args) throws IOException {

	String csvFilePath = "C:\\Users\\Code2care\\Desktop\\file\\data.csv";
	String line = null;
	BufferedReader bufferedReader = null;

	try {
		File csvFile = new File(csvFilePath);
		FileReader fileReader = new FileReader(csvFile, StandardCharsets.UTF_8);
		bufferedReader = new BufferedReader(fileReader);

		while ((line = bufferedReader.readLine()) != null) {

			String[] csvLineElements = line.split(",");
			
			for (int i = 0; i < csvLineElements.length; i++) {
				System.out.println(csvLineElements[i]);
			}
		}
	}

	catch (IOException e) {
		System.out.println("Error Occured while parsing csv file.");
		e.printStackTrace();
	} finally {
		bufferedReader.close();
	}

}

}
CSV line to be read using Java code for parsing
CSV line to be read using Java code for parsing
CSV text File:
SrNo,Name,Score
1,Mike,200
2,Alex,400
3,Steve,500
4,Alen,530
5,Kevin,108

Steps to parsing a CSV file using Java Code

  1. Create a String object with he absolute path of the csv file to be read,
  2. Create a FileReader class object, this class is used to reads text from character files using a default buffer size. Note if you are dealing with UTF-8 encoding make sure you use constructor FileReader(File file, Charset charset). Note that FileReader is meant for reading streams of characters to read streams of raw bytes you should use FileInputStream.
  3. Create an object of BufferedReader to read text from a character-input stream, we pass the FileReader object to its constructor.
  4. Now using a while loop iterate while bufferedReader.readLine() is not null.
  5. In the while loop, read the elements of the line by splitting the line by comma regex.
  6. Now you have access to each element of a CSV file.
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency