Reading and Writing CSV Files in Java

java dev.to

What is File Handling in Java?
File handling means reading data from a file or writing data into a file using a Java program.

Java provides several classes for file handling:

  • FileReader

  • BufferedReader

  • FileWriter

  • BufferedWriter

File handling is useful when we want to store data permanently instead of keeping it only in program memory.

What is a CSV File?

  • CSV stands for Comma-Separated Values.

  • A CSV file stores data in rows and columns, with each value separated by a comma.

Example:

id,name,course,mark
101,Arun,Java,85
102,Priya,Java,90
103,Karthik,React,78
104,Divya,Java,95
Enter fullscreen mode Exit fullscreen mode

What is FileReader?

 FileReader is used to read character data from a file.
Enter fullscreen mode Exit fullscreen mode

Example:

 FileReader fr = new FileReader("students.csv");
Enter fullscreen mode Exit fullscreen mode

It allows Java to access the contents of the file.

In simple words:

    FileReader is used to read data from a file.
Enter fullscreen mode Exit fullscreen mode

What is BufferedReader?

    BufferedReader is used to read text efficiently, especially one line at a time.
Enter fullscreen mode Exit fullscreen mode

Example:
BufferedReader br =
new BufferedReader(new FileReader("students.csv"));

We can read each line using:

String line = br.readLine();
Enter fullscreen mode Exit fullscreen mode

For a CSV project, BufferedReader is very useful because each CSV record is stored as a separate line.

What is ArrayList?

ArrayList is a collection in Java used to store multiple objects.

In our project:

ArrayList students = new ArrayList<>();

It stores multiple Student objects.

For example:
Student 1 → Arun
Student 2 → Priya
Student 3 → Karthik
Student 4 → Divya

FileWriter

FileWriter is a Java class used to write character data into a file.
Example:

 import java.io.FileWriter;
 import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {

    FileWriter fw = new FileWriter("output.txt");

    fw.write("Hello Java");

    fw.close();
  }
 }
Enter fullscreen mode Exit fullscreen mode

This creates output.txt and writes:

   Hello Java
Enter fullscreen mode Exit fullscreen mode

**Simple meaning

FileWriter → writes data into a file.**

BufferedWriter

  BufferedWriter is used to write text efficiently to a file. It also provides useful methods such as newLine().
Enter fullscreen mode Exit fullscreen mode

Example

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {

    BufferedWriter bw =
    new BufferedWriter(new FileWriter("output.txt"));

    bw.write("Hello Java");
    bw.newLine();
    bw.write("Welcome to File Handling");

    bw.close();
   }
}
Enter fullscreen mode Exit fullscreen mode

The output file contains:
Hello Java
Welcome to File Handling

Simple meaning
BufferedWriter → writes text efficiently and allows easy line-by-line writing.

Source: dev.to

arrow_back Back to Tutorials