Answers for "Working with text - write text by lines"

0

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);
        }
    }
}
Posted by: Guest on April-25-2021

Code answers related to "Working with text - write text by lines"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language