Methods and Techniques for Working with CSV Files in Java
In the world of data processing, CSV files are among the simplest and most ubiquitous file formats. Their straightforward structure—a grid of rows and columns represented in plain text—makes them suitable for transferring data across applications...

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVReaderExample {
public static void main(String[] args) {
String filePath = "data.csv"; // Replace with your CSV file path
String line;
String delimiter = ","; // Assumes a comma as the delimiter
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
while ((line = br.readLine()) != null) {
String[] fields = line.split(delimiter);
for (String field : fields) {
System.out.print(field + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- FileReader and BufferedReader: Efficiently reads the file line by line.
- String.split(): Splits the row into individual fields based on the delimiter.
- Edge Cases: This method doesn't handle fields enclosed in quotes or commas within fields. For such cases, a library is preferred.
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.Reader;
import java.util.List;
public class ApacheCSVExample {
public static void main(String[] args) {
String filePath = "data.csv"; // Replace with your CSV file path
try (Reader reader = new FileReader(filePath)) {
CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader);
List<CSVRecord> records = parser.getRecords();
for (CSVRecord record : records) {
System.out.println("Name: " + record.get("Name"));
System.out.println("Age: " + record.get("Age"));
System.out.println("Country: " + record.get("Country"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Header Mapping: Supports mapping fields by their headers.
- Quoted Fields: Automatically parses fields enclosed in quotes.
- Custom Delimiters: Easily handles delimiters other than commas.
import java.io.FileWriter;
import java.io.IOException;
public class CSVWriterExample {
public static void main(String[] args) {
String filePath = "output.csv";
String[] headers = {"Name", "Age", "Country"};
String[][] data = {
{"Alice", "30", "USA"},
{"Bob", "25", "UK"},
{"Charlie", "35", "Canada"}
};
try (FileWriter writer = new FileWriter(filePath)) {
// Write headers
writer.append(String.join(",", headers));
writer.append(" ");
// Write rows
for (String[] row : data) {
writer.append(String.join(",", row));
writer.append(" ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- FileWriter: Writes data directly to a file.
- String.join(): Joins fields with commas to create rows.
- Efficiency: Suitable for simple use cases but lacks error handling for special characters.
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class OpenCSVWriteExample {
public static void main(String[] args) {
String filePath = "output.csv";
try (CSVWriter writer = new CSVWriter(new FileWriter(filePath))) {
String[] header = {"Name", "Age", "Country"};
String[] record1 = {"Alice", "30", "USA"};
String[] record2 = {"Bob", "25", "UK"};
String[] record3 = {"Charlie", "35", "Canada"};
writer.writeNext(header);
writer.writeNext(record1);
writer.writeNext(record2);
writer.writeNext(record3);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Built-In Escaping: Automatically escapes special characters.
- Ease of Use: Simplifies appending rows.
- Apache Commons CSV: Best for flexibility and complex files.
- OpenCSV: Ideal for simplicity and ease of use.
Read more at : Methods and Techniques for Working with CSV Files in Java
About the author
I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
Comments
Join the discussionNo comments yet. Be the first to comment.





