From bf648325a31faf9152195462ed42ae9e14ec63b2 Mon Sep 17 00:00:00 2001 From: Amir Ali Amiri Date: Mon, 15 Jun 2026 15:51:13 +0330 Subject: [PATCH] Implement exception handling for RuntimeExceptions and complete FileService TODOs --- .idea/misc.xml | 2 +- src/main/java/org/Exceptions/FileService.java | 66 ++++++++++++------- userInformation.txt | 2 +- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d2b5d0f..115fd38 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/org/Exceptions/FileService.java b/src/main/java/org/Exceptions/FileService.java index 3aa276f..f77e135 100644 --- a/src/main/java/org/Exceptions/FileService.java +++ b/src/main/java/org/Exceptions/FileService.java @@ -3,6 +3,12 @@ package org.Exceptions; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; +import java.io.FileWriter; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.IOException; +import java.text.ParseException; public class FileService { @@ -45,46 +51,58 @@ public class FileService { public static String writeFile(UserEntity user) { - //TODO: Uncomment the lines below. - //TODO: Use your knowledge of exception handling to make the code work as expected + // باز کردن فایل در حالت append (افزودن به انتهای فایل) + // استفاده از try-with-resources باعث میشه فایل بعد از اتمام کار خودبخود بسته بشه + try (FileWriter fw = new FileWriter("userInformation.txt", true); + BufferedWriter bw = new BufferedWriter(fw)) { - /* Uncomment this section - //TODO: handle the exception - FileWriter fw = new FileWriter("userInformation.txt", true); - BufferedWriter bw = new BufferedWriter(fw); + // نوشتن اطلاعات متنی کاربر درون فایل + bw.write(user.toString()); + bw.newLine(); // اضافه کردن یک خط جدید برای کاربر بعدی + + } catch (IOException e) { + // مدیریت خطای ورودی/خروجی در زمان نوشتن فایل + System.err.println("خطا در ذخیره اطلاعات کاربر در فایل: " + e.getMessage()); + return "failed to write user information"; + } - bw.write(user.toString()); - bw.close(); - */ return "user information written to file"; } public static void readFile() { - //TODO: Uncomment the lines below. - //TODO: Use your knowledge of exception handling to make the code work as expected + // خواندن خط به خط فایل با بافر برای کارایی بالاتر + try (FileReader file = new FileReader("userInformation.txt"); + BufferedReader fileInput = new BufferedReader(file)) { - /* Uncomment this section - FileReader file = new FileReader("userInformation.txt"); + String line; + // خواندن خطوط فایل تا زمانی که به انتهای فایل نرسیدیم یا ۳ خط اول را خواندیم + for (int counter = 0; counter < 3; counter++) { + line = fileInput.readLine(); + if (line == null) { + break; // اگر خطوط فایل کمتر از ۳ تا بود، حلقه متوقف شود + } + System.out.println(line); + } - BufferedReader fileInput = new BufferedReader(file); - - for (int counter = 0; counter < 3; counter++) - System.out.println(fileInput.readLine()); - - fileInput.close(); - */ + } catch (IOException e) { + // این بلاک هم خطای پیدا نشدن فایل و هم خطای خواندن را پوشش می‌دهد + System.err.println("خطا در خواندن اطلاعات از فایل (احتمالا فایل هنوز ساخته نشده): " + e.getMessage()); + } } public static Date parseDate(String date) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - Date dob = null; - //TODO: Uncomment this line. - //TODO: Use your knowledge of exception handling to make the code work as expected - //dob = formatter.parse(date); + try { + // تبدیل متن ورودی به شیء تاریخ واقعی در جاوا + dob = formatter.parse(date); + } catch (ParseException e) { + // مدیریت خطای فرمت نامعتبر تاریخ ورودی + System.err.println("فرمت تاریخ وارد شده اشتباه است. لطفا از الگوی yyyy-MM-dd استفاده کنید."); + } return dob; } diff --git a/userInformation.txt b/userInformation.txt index 29adda4..8b13789 100644 --- a/userInformation.txt +++ b/userInformation.txt @@ -1 +1 @@ -HELLO! \ No newline at end of file + -- 2.54.0