
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to read the data from a CSV file in Java?n
A CSV stands for Comma Separated Values. In a CSV file, each line contains words that are separated with a comma(,) and it is stored with a .csv extension.
We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.
CSV File
Example
import java.io.*; public class CSVReaderTest { public static final String delimiter = ","; public static void read(String csvFile) { try { File file = new File(csvFile); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line = ""; String[] tempArr; while((line = br.readLine()) != null) { tempArr = line.split(delimiter); for(String tempStr : tempArr) { System.out.print(tempStr + " "); } System.out.println(); } br.close(); } catch(IOException ioe) { ioe.printStackTrace(); } } public static void main(String[] args) { // csv file to read String csvFile = "C:/Temp/Technology.csv"; CSVReaderTest.read(csvFile); } }
Output
"JAVA" "PYTHON" "JAVASCRIPT" "SELENIUM" "SCALA"
- Related Articles
- How to read data from .csv file in Java?
- How to read data from *.CSV file using JavaScript?
- How to read the data from a file in Java?
- Write data from/to a .csv file in java
- How to read the data from a properties file in Java?\n
- How to read CSV file in Python?
- How to read data in from a file to String using java?
- How to write data to .csv file in Java?
- How to read/write data from/to .properties file in Java?
- How to read data from one file and print to another file in Java?
- Writing data from database to .csv file
- How to import csv file data from Github in R?
- How to read data from a file using FileInputStream?
- How to read a Pandas CSV file with no header?
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files

Advertisements