- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write data to .csv file in Java?
A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to write the contents of a .csv file using a Java program.
Maven dependency
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>4.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
The CSVWriter class of the com.opencsv package represents a simple csv writer. While instantiating this class you need to pass a Writer object representing the file, to which you want to write the data, as a parameter to its constructor.
It provides methods named writeAll() and writeNext() to write data to a .csv file.
Using the writeNext() method
The writeNext() method of the CSVWriter class writes the next line to the .csv file
Example
Following Java program demonstrates how to write data to a .csv file using the writeNext() method.
import java.io.FileWriter; import com.opencsv.CSVWriter; public class WritingToCSV { public static void main(String args[]) throws Exception { //Instantiating the CSVWriter class CSVWriter writer = new CSVWriter(new FileWriter("D://output.csv")); //Writing data to a csv file String line1[] = {"id", "name", "salary", "start_date", "dept"}; String line2[] = {"1", "Krishna", "2548", "2012-01-01", "IT"}; String line3[] = {"2", "Vishnu", "4522", "2013-02-26", "Operations"}; String line4[] = {"3", "Raja", "3021", "2016-10-10", "HR"}; String line5[] = {"4", "Raghav", "6988", "2012-01-01", "IT"}; //Writing data to the csv file writer.writeNext(line1); writer.writeNext(line2); writer.writeNext(line3); writer.writeNext(line4); //Flushing data from writer to file writer.flush(); System.out.println("Data entered"); } }
Output
Data entered
Using the writeAll() method
This method accepts a List object (of String array type) containing the contents to be written and, writes them to the .csv file at once.
Example
Following Java program demonstrates write contents to a .csv file using the writeAll() method.
import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import com.opencsv.CSVWriter; public class WritingToCSV { public static void main(String args[]) throws Exception { //Instantiating the CSVWriter class CSVWriter writer = new CSVWriter(new FileWriter("D://output.csv")); //Writing data to a csv file String line1[] = {"id", "name", "salary", "start_date", "dept"}; String line2[] = {"1", "Krishna", "2548", "2012-01-01", "IT"}; String line3[] = {"2", "Vishnu", "4522", "2013-02-26", "Operations"}; String line4[] = {"3", "Raja", "3021", "2016-10-10", "HR"}; String line5[] = {"4", "Raghav", "6988", "2012-01-01", "IT"}; //Instantiating the List Object List list = new ArrayList(); list.add(line1); list.add(line2); list.add(line3); list.add(line4); list.add(line5); //Writing data to the csv file writer.writeAll(list); writer.flush(); System.out.println("Data entered"); } }
Output
Data entered
- Related Articles
- Write data from/to a .csv file in java
- How to read data from .csv file in Java?
- How to read the data from a CSV file in Java?\n
- Python - How to write pandas dataframe to a CSV file
- How to import csv file data from Github in R?
- How to read data from *.CSV file using JavaScript?
- How to read/write data from/to .properties file in Java?
- Writing data from database to .csv file
- How to append data into a CSV file using PowerShell?
- How to import csv file in PHP?
- How to read CSV file in Python?
- Python Tkinter – How to export data from Entry Fields to a CSV file?
- How to convert JSON file to CSV file using PowerShell?
- How to convert CSV File to PDF File using Python?
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
