Working with text - write text by lines
public class FileWriter {
public void writeLines(List<Employee> employees, BufferedWriter writer) {
try {
for (String employee : employees) {
writer.write(employee.getName() + "," + employee.getYearOfBirth());
}
} catch (IOException ioe) {
throw new IllegalStateException("Can not read file", ioe);
}
}
public static void main(String[] args) {
List<Employee> employees = List.of(new Employee("John Doe", 1970), new Employee("Jack Doe", 1980));
try (BufferedWriter writer = Files.newBufferedWriter(Path.of("data.csv"))) {
new FileWriter().writeLines(employees, writer);
} catch (IOException ioe) {
throw new IllegalStateException("Can not write file", ioe);
}
}
}