Created
November 26, 2025 05:51
-
-
Save untainsYD/e5a1c94bddcbb1146768aa3d5be04f0a to your computer and use it in GitHub Desktop.
Java advanced: Laboratory 3, Task4: class GroupFileManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ua.inf.iwanoff.java.advanced.lab3.service; | |
| import com.thoughtworks.xstream.XStream; | |
| import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; | |
| import com.thoughtworks.xstream.security.AnyTypePermission; | |
| import org.apache.logging.log4j.LogManager; | |
| import org.apache.logging.log4j.Logger; | |
| import org.json.JSONArray; | |
| import org.json.JSONObject; | |
| import ua.inf.iwanoff.java.advanced.lab3.model.AcademicGroup; | |
| import ua.inf.iwanoff.java.advanced.lab3.model.Student; | |
| import java.io.IOException; | |
| import java.io.PrintWriter; | |
| import java.nio.charset.StandardCharsets; | |
| import java.nio.file.Files; | |
| import java.nio.file.Paths; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class GroupFileManager { | |
| private static final Logger logger = LogManager.getLogger(GroupFileManager.class); | |
| // --- 1. Text File (Stream API) --- | |
| public static void writeToText(AcademicGroup group, String filename) { | |
| logger.info("Writing group to TEXT file: " + filename); | |
| try (PrintWriter writer = new PrintWriter(filename, StandardCharsets.UTF_8)) { | |
| writer.println(group.getGroupName()); | |
| group.getStudents() | |
| .forEach(s -> writer.println(s.getName() + ";" + s.getAge() + ";" + s.getAverageGrade())); | |
| } catch (IOException e) { | |
| logger.error("Error writing text file", e); | |
| } | |
| } | |
| public static AcademicGroup readFromText(String filename) { | |
| logger.info("Reading group from TEXT file: " + filename); | |
| try (Stream<String> lines = Files.lines(Paths.get(filename), StandardCharsets.UTF_8)) { | |
| List<String> list = lines.collect(Collectors.toList()); | |
| if (list.isEmpty()) | |
| return null; | |
| String groupName = list.get(0); | |
| List<Student> students = list.stream() | |
| .skip(1) | |
| .map(line -> { | |
| String[] parts = line.split(";"); | |
| return new Student(parts[0], Integer.parseInt(parts[1]), Double.parseDouble(parts[2])); | |
| }) | |
| .collect(Collectors.toList()); | |
| return new AcademicGroup(groupName, students); | |
| } catch (IOException e) { | |
| logger.error("Error reading text file", e); | |
| return null; | |
| } | |
| } | |
| // --- 2. XML (XStream) --- | |
| public static void serializeToXML(AcademicGroup group, String filename) { | |
| logger.info("Serializing group to XML: " + filename); | |
| XStream xStream = new XStream(); | |
| xStream.alias("group", AcademicGroup.class); | |
| xStream.alias("student", Student.class); | |
| try (PrintWriter writer = new PrintWriter(filename, StandardCharsets.UTF_8)) { | |
| writer.print(xStream.toXML(group)); | |
| } catch (IOException e) { | |
| logger.error("Error writing XML", e); | |
| } | |
| } | |
| public static AcademicGroup deserializeFromXML(String filename) { | |
| logger.info("Deserializing group from XML: " + filename); | |
| XStream xStream = new XStream(); | |
| xStream.addPermission(AnyTypePermission.ANY); | |
| xStream.alias("group", AcademicGroup.class); | |
| xStream.alias("student", Student.class); | |
| try { | |
| return (AcademicGroup) xStream.fromXML(Paths.get(filename).toFile()); | |
| } catch (Exception e) { | |
| logger.error("Error reading XML", e); | |
| return null; | |
| } | |
| } | |
| // --- 3. JSON (XStream) --- | |
| public static void serializeToJSON_XStream(AcademicGroup group, String filename) { | |
| logger.info("Serializing group to JSON (XStream): " + filename); | |
| XStream xStream = new XStream(new JettisonMappedXmlDriver()); | |
| xStream.alias("group", AcademicGroup.class); | |
| xStream.alias("student", Student.class); | |
| try (PrintWriter writer = new PrintWriter(filename, StandardCharsets.UTF_8)) { | |
| writer.print(xStream.toXML(group)); | |
| } catch (IOException e) { | |
| logger.error("Error writing JSON (XStream)", e); | |
| } | |
| } | |
| public static AcademicGroup deserializeFromJSON_XStream(String filename) { | |
| logger.info("Deserializing group from JSON (XStream): " + filename); | |
| XStream xStream = new XStream(new JettisonMappedXmlDriver()); | |
| xStream.addPermission(AnyTypePermission.ANY); | |
| xStream.alias("group", AcademicGroup.class); | |
| xStream.alias("student", Student.class); | |
| try { | |
| return (AcademicGroup) xStream.fromXML(Paths.get(filename).toFile()); | |
| } catch (Exception e) { | |
| logger.error("Error reading JSON (XStream)", e); | |
| return null; | |
| } | |
| } | |
| // --- 4. JSON (Org.json) - Завдання 1.5 --- | |
| public static void writeJsonOrg(AcademicGroup group, String filename) { | |
| logger.info("Writing group to JSON (org.json): " + filename); | |
| // Створення JSON структури | |
| JSONObject jsonGroup = new JSONObject(); | |
| jsonGroup.put("groupName", group.getGroupName()); | |
| JSONArray jsonStudents = new JSONArray(); | |
| for (Student s : group.getStudents()) { | |
| JSONObject jsonStudent = new JSONObject(); | |
| jsonStudent.put("name", s.getName()); | |
| jsonStudent.put("age", s.getAge()); | |
| jsonStudent.put("avg", s.getAverageGrade()); | |
| jsonStudents.put(jsonStudent); | |
| } | |
| jsonGroup.put("students", jsonStudents); | |
| try (PrintWriter writer = new PrintWriter(filename, StandardCharsets.UTF_8)) { | |
| // indentFactor=2 для красивого форматування | |
| writer.print(jsonGroup.toString(2)); | |
| } catch (IOException e) { | |
| logger.error("Error writing JSON (org.json)", e); | |
| } | |
| } | |
| public static AcademicGroup readJsonOrg(String filename) { | |
| logger.info("Reading group from JSON (org.json): " + filename); | |
| try { | |
| String content = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8); | |
| JSONObject jsonGroup = new JSONObject(content); | |
| String groupName = jsonGroup.getString("groupName"); | |
| JSONArray jsonStudents = jsonGroup.getJSONArray("students"); | |
| List<Student> students = new ArrayList<>(); | |
| for (int i = 0; i < jsonStudents.length(); i++) { | |
| JSONObject obj = jsonStudents.getJSONObject(i); | |
| students.add(new Student( | |
| obj.getString("name"), | |
| obj.getInt("age"), | |
| obj.getDouble("avg"))); | |
| } | |
| return new AcademicGroup(groupName, students); | |
| } catch (IOException e) { | |
| logger.error("Error reading JSON (org.json)", e); | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment