
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Working with csv files in Java
OpenCSV has to be installed first, which is a parser library for Java. The dependency has to be mentioned in the pom.xml file in the maven project. After that, the below code can be utilized.
Example
import java.io.FileReader; import java.io.*; public class Demo{ public static void readDataLineByLine(String file){ try{ FileReader my_filereader = new FileReader(file); CSVReader csvReader = new CSVReader(my_filereader); String[] nextRecord; while ((nextRecord = csvReader.readNext()) != null){ for (String cell : nextRecord){ System.out.print(Output + "\t"); } System.out.println(); } } catch (Exception e){ e.printStackTrace(); } } }
Output
Prints data in a csv file line by line
A class named Demo contains a function named ‘readDataLineByLine’ that takes a file as parameter. A FileReader instance is created, and a CSVReader instance is created, that reads the elements in the CSV file. Every line is read one by one and displayed on the screen. This is written in the try block, and exceptions (if any) are caught in the ‘catch’ block.
- Related Questions & Answers
- Working with csv files in Python Programming
- Working with zip files in Python
- Working with PDF files in Python?
- How to Handle Large CSV files with Pandas?
- Python Pandas- Create multiple CSV files from existing CSV file
- How to read CSV files in Golang?
- How to read and parse CSV files in C++?
- Python - Read all CSV files in a folder in Pandas?
- Working with BigInteger Values in Java
- Working with BigDecimal values in Java
- Working with Java inside Docker Container
- Working with simple groups in Java Regular Expressions
- Working with Matcher.start() method in Java Regular Expressions
- Working with Matcher.end() method in Java Regular Expressions
- How to Merge multiple CSV Files into a single Pandas dataframe ?
Advertisements