How to Read and Write CSV files in Java using Eclipse IDE

Overview

In this tutorial, we show you how to read and write CSV files in Java using FileReader and FileWriter with Eclipse IDE.



How to Read and Write CSV files in Java


Watch Tutorial




Project Structure

The following screenshot shows final structure of the project.
Project Structure of read and write csv files

Creating the Project

  • Launch Eclipse IDE.
  • Go to File-> New-> Others... Select Java Project under Java category then click Next.
  • Type "ReadWriteCsvTutorial" in the "Project Name" field
  • Click Finish.
User.java
Create a User class under com.jackrutorial package and write the following code in it.

package com.jackrutorial;

public class User {
 private int id;
 private String firstName;
 private String lastName;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 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;
 }
}

CsvReaderWriter.java
Create a CsvReaderWriter class under com.jackrutorial package and write the following code in it.

package com.jackrutorial;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class CsvReaderWriter {
 
 public static void main(String[] args) {
  String filePath = "C:\\java\\csv\\user.csv";
  
  System.out.println("starting write user.csv file: " + filePath);
  writeCsv(filePath);
  
  System.out.println("starting read user.csv file");
  readCsv(filePath);
 }
 
 public static void writeCsv(String filePath) {
  List<User> users = new ArrayList<User>();
  
  //create demo Users
  User user = new User();
  user.setId(1);
  user.setFirstName("Jack");
  user.setLastName("Rutorial 1");
  users.add(user);
  
  user = new User();
  user.setId(2);
  user.setFirstName("Jack");
  user.setLastName("Rutorial 2");
  users.add(user);
  
  user = new User();
  user.setId(3);
  user.setFirstName("Jack");
  user.setLastName("Rutorial 3");
  users.add(user);
  
  FileWriter fileWriter = null;
  try {
   fileWriter = new FileWriter(filePath);
   
   fileWriter.append("Id, First Name, Last Name\n");
   for(User u: users) {
    fileWriter.append(String.valueOf(u.getId()));
    fileWriter.append(",");
    fileWriter.append(u.getFirstName());
    fileWriter.append(",");
    fileWriter.append(u.getLastName());
    fileWriter.append("\n");
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   try {
    fileWriter.flush();
    fileWriter.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void readCsv(String filePath) {
  BufferedReader reader = null;
  
  try {
   List<User> users = new ArrayList<User>();
   String line = "";
   reader = new BufferedReader(new FileReader(filePath));
   reader.readLine();
   
   while((line = reader.readLine()) != null) {
    String[] fields = line.split(",");
    
    if(fields.length > 0) {
     User user = new User();
     user.setId(Integer.parseInt(fields[0]));
     user.setFirstName(fields[1]);
     user.setLastName(fields[2]);
     users.add(user);
    }
   }
   
   for(User u: users) {
    System.out.printf("[userId=%d, firstName=%s, lastName=%s]\n", u.getId(), u.getFirstName(), u.getLastName());
   }
   
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   try {
    reader.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  
 }
}

Run the Program

  • Right click to the class select Run As -> Java Application.

Output


starting write user.csv file: C:\java\csv\user.csv
starting read user.csv file
[userId=1, firstName=Jack, lastName=Rutorial 1]
[userId=2, firstName=Jack, lastName=Rutorial 2]
[userId=3, firstName=Jack, lastName=Rutorial 3]

user.csv

Id, First Name, Last Name
1,Jack,Rutorial 1
2,Jack,Rutorial 2
3,Jack,Rutorial 3

Previous Post
Next Post

post written by: