How to Export data into a CSV File

We already seen how to Parse a CSV file, in this example we will learn how to Write/Export data to CSV File using simple Java.

Employee.java

Employee class is a simple POJO to hold Employee details such as EmpId, FirstName, LastName, Salary.

package com.javainterviewpoint;

public class Employee 
{
    private int empId;
    private String firstName;
    private String lastName;
    private int salary;
    
    public Employee(int empId, String firstName, String lastName, int salary) {
        super();
        this.empId = empId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
    }
    
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [empId=" + empId + ", firstName=" + firstName
                + ", lastName=" + lastName + ", salary=" + salary + "]";
    }
}

Writing a CSV File

We will be using simple FileWriter here to export our Employee object to Employee.csv

package com.javainterviewpoint;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WriteCSVFile 
{
	//Delimiters which has to be in the CSV file
	private static final String COMMA_DELIMITER = ",";
    private static final String LINE_SEPARATOR = "\n";
    
    //File header
    private static final String HEADER = "EmployeeID,FirstName,LastName,Salary";
    
    public static void main(String args[])
    {
    	//Creating Employee objects
    	Employee emp1 = new Employee(1,"FirstName1","LastName1",10000);
    	Employee emp2 = new Employee(2,"FirstName2","LastName2",20000);
    	Employee emp3 = new Employee(3,"FirstName3","LastName3",30000);
    	Employee emp4 = new Employee(4,"FirstName4","LastName4",40000);
    	Employee emp5 = new Employee(5,"FirstName5","LastName5",50000);
    	
    	//Add Employee objects to a list
    	List empList = new ArrayList();
    	empList.add(emp1);
    	empList.add(emp2);
    	empList.add(emp3);
    	empList.add(emp4);
    	empList.add(emp5);
    	
    	FileWriter fileWriter = null;
    	
    	try
    	{
    		fileWriter = new FileWriter("Employee.csv");
    		
    		//Adding the header
    		fileWriter.append(HEADER);
    		//New Line after the header
    		fileWriter.append(LINE_SEPARATOR);
    		
    		//Iterate the empList
    		Iterator it = empList.iterator();
    		while(it.hasNext())
    		{
    			Employee e = (Employee)it.next();
    			fileWriter.append(String.valueOf(e.getEmpId()));
    			fileWriter.append(COMMA_DELIMITER);
    			fileWriter.append(e.getFirstName());
    			fileWriter.append(COMMA_DELIMITER);
    			fileWriter.append(e.getLastName());
    			fileWriter.append(COMMA_DELIMITER);
    			fileWriter.append(String.valueOf(e.getSalary()));
    			fileWriter.append(LINE_SEPARATOR);
    		}
    		System.out.println("Write to CSV file Succeeded!!!");
    	}
    	catch(Exception ee)
    	{
    		ee.printStackTrace();
    	}
    	finally
    	{
    		try
    		{
    			fileWriter.close();
    		}
    		catch(IOException ie)
    		{
    			System.out.println("Error occured while closing the fileWriter");
    			ie.printStackTrace();
    		}
    	}
    }
}

Output : 

Employee.csv will be created

EmployeeID,FirstName,LastName,Salary
1,FirstName1,LastName1,10000
2,FirstName2,LastName2,20000
3,FirstName3,LastName3,30000
4,FirstName4,LastName4,40000
5,FirstName5,LastName5,50000

Leave a Reply

Your email address will not be published. Required fields are marked *