Writing to a CSV file in Java

In this blog post, we will discuss how to write data to a CSV (Comma-Separated Values) file using Java programming language. Writing to a CSV file is a common requirement in many applications, especially when dealing with large amounts of data that needs to be exported or saved in a format that can be easily shared and read by other programs.

To begin with, let’s first understand the structure of a CSV file. A CSV file consists of records (rows) separated by line breaks, with each record containing fields (columns) separated by commas. This format allows for easy parsing and manipulation of data.

To write data to a CSV file in Java, we can make use of the BufferedWriter class from the java.io package. Here’s an example code snippet that demonstrates how to write a list of records to a CSV file:

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

public class CSVWriter {
    public static void writeToFile(List<String[]> records, String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String[] record : records) {
                writer.write(String.join(",", record));
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String[]> records = List.of(
                new String[]{"Name", "Age", "City"},
                new String[]{"John Doe", "35", "New York"},
                new String[]{"Jane Smith", "28", "San Francisco"}
        );
        String filePath = "data.csv";
        writeToFile(records, filePath);
        System.out.println("Data written to CSV file successfully.");
    }
}

In the above code, we define a CSVWriter class with a method writeToFile that takes a list of string arrays (records) and a file path as parameters. Inside the method, we use a BufferedWriter to write the records to the CSV file. We iterate over each record, join the fields with commas using String.join(",", record), and write it to the file using the write method. We also append a line break using newLine method to separate each record. The file is closed automatically using the try-with-resources statement.

In the main method, we define a sample list of records and a file path. We then call the writeToFile method passing in the records and file path, and display a success message once the data is written to the CSV file.

By following this approach, you can easily write data to a CSV file in Java. This can be handy when working with data processing, reporting, or any scenario where interoperability with other systems is required.

#Java #CSV #FileIO #DataProcessing