Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Comma-separated value (CSV) files are easy to work with and applicable in various applications regarding data storing and exchanging. Java developers, at some point, have to export data to CSVs when handling data structures like HashMaps.

In this tutorial, we’ll learn how to write HashMap to a CSV file.

2. Writing HashMap to CSV Manually

To write data into the “employee_data.csv” file, we’ll use the help of the FileWriter class. Afterward, each row of employee data is inserted into separate EmployeeData cells. Here’s the code that accomplishes this:

Map<String, String> employeeData = new HashMap<>();
employeeData.put("Name", "John Doe");
employeeData.put("Title", "Software Engineer");
employeeData.put("Department", "Engineering");
employeeData.put("Salary", "75000");
try (FileWriter csvWriter = new FileWriter("employee_data.csv")) {
    // Write header row
    csvWriter.append("Name,Title,Department,Salary\n");

    // Write data row
    csvWriter.append(employeeData.get("Name")).append(",");
    csvWriter.append(employeeData.get("Title")).append(",");
    csvWriter.append(employeeData.get("Department")).append(",");
    csvWriter.append(employeeData.get("Salary")).append("\n");

    // Close the csvWriter to save the data
    csvWriter.close();
} catch (IOException e) {
    e.printStackTrace();
}

In the above code, we created row headers and then traversed the employeeData Hashmap to append each key value as a CSV row delimited by a comma. After completing the data write operation, we close the csvWriter to save the data. And then, finally, the code tries to handle exceptions.

3. Writing HashMap to CSV Using Apache Commons CSV

Working with CSV files in Java is a robust and efficient endeavor using the Apache Commons CSV library. To write HashMap data to a CSV file, we should first add the following dependency to our project’s pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.10.0</version>
</dependency>

Now, let’s see how to retrieve data from a CSV using the Apache Commons library:

try (CSVPrinter csvPrinter = new CSVPrinter(new FileWriter("employee_data2.csv"), CSVFormat.DEFAULT)) {
    // Write header row
    csvPrinter.printRecord("Name", "Title", "Department", "Salary");

    // Write data row
    csvPrinter.printRecord(employeeData.get("Name"), employeeData.get("Title"), employeeData.get("Department"), employeeData.get("Salary"));
} catch (IOException e) {
    e.printStackTrace();
}

// Ensure the CSV file exists
assertTrue(new File("employee_data2.csv").exists());

In the above code, we initialize a CSVPrinter and create a new FileWriter object to identify where the output CSV file is expected. Subsequently, we iterate through the HashMap using the CSVPrinter to put its content into the CSV file. Lastly, we close both CSVPrinter and FileWriter to ensure that the data is properly flushed and saved as required.

4. Conclusion

In conclusion, we have learned that it is possible for us to generate and write HashMap into a CSV File by means of assertion while asserting the proof of our implementation in Java.

To be specific, this skill is used in various data-related activities like transferring data towards more detailed analyses, report creation, and migration.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW