Implement exception handling for RuntimeExceptions and complete FileService TODOs #1

Open
AmirAli_Amiri wants to merge 1 commits from develop into main
3 changed files with 44 additions and 26 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" project-jdk-name="homebrew-26" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+42 -24
View File
@@ -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;
}
+1 -1
View File
@@ -1 +1 @@
HELLO!