From 4dadefd87f644426d53ef8a47581a4e3a197997e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=93=D0=B5?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D0=BD?= Date: Mon, 31 Aug 2026 22:17:51 +0400 Subject: [PATCH] feat(io): add StudentCsvWriter - add CSV writer for individual students - support sequential writing of student lists - preserve existing CSV data and avoid duplicate headers - reject missing files with IOException --- .../java/ru/project/io/StudentCsvWriter.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app/src/main/java/ru/project/io/StudentCsvWriter.java diff --git a/app/src/main/java/ru/project/io/StudentCsvWriter.java b/app/src/main/java/ru/project/io/StudentCsvWriter.java new file mode 100644 index 0000000..8795fbc --- /dev/null +++ b/app/src/main/java/ru/project/io/StudentCsvWriter.java @@ -0,0 +1,88 @@ +package ru.project.io; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import ru.project.collection.MyList; +import ru.project.student.Student; + +public class StudentCsvWriter { + + private static final String GROUP_NUMBER_HEADER = "groupNumber"; + private static final String AVERAGE_GRADE_HEADER = "averageGrade"; + private static final String RECORD_BOOK_NUMBER_HEADER = "recordBookNumber"; + + private final String absoluteFilePath; + + public StudentCsvWriter(String absoluteFilePath) { + this.absoluteFilePath = absoluteFilePath; + } + + public void write(Student student) throws IOException { + ensureFileExists(); + + boolean fileIsEmpty = isFileEmpty(); + + try (CSVPrinter printer = createPrinter()) { + if (fileIsEmpty) { + writeHeader(printer); + } + + writeStudent(printer, student); + } + } + + public void write(MyList[] students) throws IOException { + ensureFileExists(); + + boolean fileIsEmpty = isFileEmpty(); + + try (CSVPrinter printer = createPrinter()) { + if (fileIsEmpty) { + writeHeader(printer); + } + + for (MyList studentList : students) { + for (int i = 0; i < studentList.size(); i++) { + writeStudent(printer, studentList.get(i)); + } + } + } + } + + private CSVPrinter createPrinter() throws IOException { + BufferedWriter writer = + new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream(absoluteFilePath, true), StandardCharsets.UTF_8)); + + return new CSVPrinter(writer, CSVFormat.DEFAULT); + } + + private void writeHeader(CSVPrinter printer) throws IOException { + printer.printRecord(GROUP_NUMBER_HEADER, AVERAGE_GRADE_HEADER, RECORD_BOOK_NUMBER_HEADER); + } + + private void writeStudent(CSVPrinter printer, Student student) throws IOException { + + printer.printRecord( + student.getGroupNumber(), student.getAverageGrade(), student.getRecordBookNumber()); + } + + private void ensureFileExists() throws IOException { + File file = new File(absoluteFilePath); + + if (!file.isFile()) { + throw new IOException("CSV file does not exist: " + absoluteFilePath); + } + } + + private boolean isFileEmpty() { + return new File(absoluteFilePath).length() == 0; + } +}