Writing formatted CSV files
After cleaning your data, you need to export it for different consumers. The analytics team wants standard CSVs, European systems expect semicolon separators, and a legacy mainframe requires headerless files. Flexible export options ensure your data integrates smoothly across diverse technical environments.
The Table and CsvWriteOptions classes have been imported for you.
Este exercício faz parte do curso
Importing Data in Java
Instruções do exercício
- Write the table as a standard CSV.
- Write with a semicolon separator.
- Write without headers for a legacy system.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
public class WriteCSVFiles {
public static void main(String[] args) {
try {
Table employeePerformance = Table.read().csv("employee_performance.csv");
System.out.println("Original data structure:");
System.out.println(employeePerformance.structure());
System.out.println("First 3 rows:");
System.out.println(employeePerformance.first(3));
// Write the table as a standard CSV
employeePerformance.____.csv("employee_performance_standard.csv");
// Write with a semicolon separator
employeePerformance.write().csv(CsvWriteOptions
.____("employee_performance_semicolon.csv")
.____(';')
.build());
// Write without headers for a legacy system
employeePerformance.write().csv(CsvWriteOptions
.builder("employee_performance_noheader.csv")
.____(____)
.build());
System.out.println("\nFiles successfully written to output directory.");
Table readBack = Table.read().csv("employee_performance_standard.csv");
System.out.println("\nVerification - Reading back semicolon-separated file:");
System.out.println(readBack.first(3));
} catch (Exception e) {
System.err.println("Error processing CSV files: " + e.getMessage());
}
}
}