how to create a CSV file in Java

0 votes
can you tell me how to create a CSV file in Java?
Feb 25 in Node-js by Nidhi
• 10,860 points
43 views

1 answer to this question.

0 votes

Using OpenCSV Library

Add OpenCSV dependency (if using Maven):

<dependency>

    <groupId>com.opencsv</groupId>

    <artifactId>opencsv</artifactId>

    <version>5.5.2</version>

</dependency>

Write CSV using OpenCSV:

import com.opencsv.CSVWriter;

import java.io.FileWriter;

import java.io.IOException;

public class OpenCSVWriter {

    public static void main(String[] args) {

        String fileName = "data.csv";

        try (CSVWriter writer = new CSVWriter(new FileWriter(fileName))) {

            String[] header = {"ID", "Name", "Age"};

            String[] row1 = {"1", "John", "25"};

            String[] row2 = {"2", "Emma", "30"};

            String[] row3 = {"3", "Michael", "22"};

            writer.writeNext(header);

            writer.writeNext(row1);

            writer.writeNext(row2);

            writer.writeNext(row3);

            System.out.println("CSV file created successfully.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

answered Feb 25 by Navya

Related Questions In Node-js

0 votes
0 answers

How to append to a file in Node?

I am trying to append a string to a ...READ MORE

Jul 9, 2020 in Node-js by kartik
• 37,520 points
1,112 views
0 votes
1 answer

How to append to a file in Node?

Hello @kartik, you can use appendFile, which creates a ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,840 points
762 views
0 votes
1 answer

How to update a value in a json file and save it through node.js?

//install ciql-json : npm i ciql-json const ciqlJson ...READ MORE

answered May 26, 2021 in Node-js by Sirus

edited 4 days ago 26,150 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,350 views
0 votes
1 answer

How to access url for the current if statement of laravel?

Hello @ subham , If you want to access the ...READ MORE

answered Aug 7, 2020 in Laravel by Niroj
• 82,840 points
1,531 views
0 votes
1 answer

How can i insert data in relation table using model?

Hello @Alisha, Try to work using the model ...READ MORE

answered Aug 24, 2020 in Java-Script by Niroj
• 82,840 points
1,493 views
0 votes
1 answer

How can I implement pagination for large datasets in an Express.js API?

Pagination is a technique used to divide ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
262 views
0 votes
1 answer

How do you implement API request validation in Express using middleware?

1. Create Middleware Function :  - Define a ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
220 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
4,082 views
0 votes
1 answer

How to replace special characters in a JSON string?

You can use the replace method in ...READ MORE

answered Dec 17, 2024 in Node-js by Navya
106 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP