From dda4d4f83b6181dcd8cbbce9a51f730c227fb521 Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:27:14 +0100 Subject: [PATCH] Added writeToFile Method to Student class --- src/main/java/org/example/Student.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 9708eb9..1f2f009 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -1,4 +1,9 @@ package org.example; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + /** * Represents an student user in the system. */ @@ -61,4 +66,14 @@ public class Student { System.out.println("Role: " + getRole()); } + + public void writeToFile(Student student, String filename) throws IOException { + try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ + String attributes = student.getName() + "\n" + student.getId() + "\n" + student.getRole(); + writer.write(attributes); + } + + } + + }