Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf648325a3 |
Generated
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -3,6 +3,12 @@ package org.Exceptions;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Scanner;
|
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 {
|
public class FileService {
|
||||||
|
|
||||||
@@ -45,46 +51,58 @@ public class FileService {
|
|||||||
|
|
||||||
|
|
||||||
public static String writeFile(UserEntity user) {
|
public static String writeFile(UserEntity user) {
|
||||||
//TODO: Uncomment the lines below.
|
// باز کردن فایل در حالت append (افزودن به انتهای فایل)
|
||||||
//TODO: Use your knowledge of exception handling to make the code work as expected
|
// استفاده از try-with-resources باعث میشه فایل بعد از اتمام کار خودبخود بسته بشه
|
||||||
|
try (FileWriter fw = new FileWriter("userInformation.txt", true);
|
||||||
/* Uncomment this section
|
BufferedWriter bw = new BufferedWriter(fw)) {
|
||||||
//TODO: handle the exception
|
|
||||||
FileWriter fw = new FileWriter("userInformation.txt", true);
|
|
||||||
BufferedWriter bw = new BufferedWriter(fw);
|
|
||||||
|
|
||||||
|
// نوشتن اطلاعات متنی کاربر درون فایل
|
||||||
bw.write(user.toString());
|
bw.write(user.toString());
|
||||||
bw.close();
|
bw.newLine(); // اضافه کردن یک خط جدید برای کاربر بعدی
|
||||||
*/
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
// مدیریت خطای ورودی/خروجی در زمان نوشتن فایل
|
||||||
|
System.err.println("خطا در ذخیره اطلاعات کاربر در فایل: " + e.getMessage());
|
||||||
|
return "failed to write user information";
|
||||||
|
}
|
||||||
|
|
||||||
return "user information written to file";
|
return "user information written to file";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void readFile() {
|
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
|
String line;
|
||||||
FileReader file = new FileReader("userInformation.txt");
|
// خواندن خطوط فایل تا زمانی که به انتهای فایل نرسیدیم یا ۳ خط اول را خواندیم
|
||||||
|
for (int counter = 0; counter < 3; counter++) {
|
||||||
|
line = fileInput.readLine();
|
||||||
|
if (line == null) {
|
||||||
|
break; // اگر خطوط فایل کمتر از ۳ تا بود، حلقه متوقف شود
|
||||||
|
}
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
BufferedReader fileInput = new BufferedReader(file);
|
} catch (IOException e) {
|
||||||
|
// این بلاک هم خطای پیدا نشدن فایل و هم خطای خواندن را پوشش میدهد
|
||||||
for (int counter = 0; counter < 3; counter++)
|
System.err.println("خطا در خواندن اطلاعات از فایل (احتمالا فایل هنوز ساخته نشده): " + e.getMessage());
|
||||||
System.out.println(fileInput.readLine());
|
}
|
||||||
|
|
||||||
fileInput.close();
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Date parseDate(String date) {
|
public static Date parseDate(String date) {
|
||||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
Date dob = null;
|
Date dob = null;
|
||||||
|
|
||||||
//TODO: Uncomment this line.
|
try {
|
||||||
//TODO: Use your knowledge of exception handling to make the code work as expected
|
// تبدیل متن ورودی به شیء تاریخ واقعی در جاوا
|
||||||
//dob = formatter.parse(date);
|
dob = formatter.parse(date);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
// مدیریت خطای فرمت نامعتبر تاریخ ورودی
|
||||||
|
System.err.println("فرمت تاریخ وارد شده اشتباه است. لطفا از الگوی yyyy-MM-dd استفاده کنید.");
|
||||||
|
}
|
||||||
|
|
||||||
return dob;
|
return dob;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
HELLO!
|
|
||||||
|
|||||||
Reference in New Issue
Block a user