87 lines
2.1 KiB
Java
87 lines
2.1 KiB
Java
package org.exceptions;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Scanner;
|
|
|
|
public class FileService {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
System.out.println("Enter you first name: ");
|
|
String firstName = scanner.nextLine();
|
|
|
|
System.out.println("Enter you last name: ");
|
|
String lastName = scanner.nextLine();
|
|
|
|
System.out.println("Enter your national code: ");
|
|
Long nationalCode = scanner.nextLong();
|
|
scanner.nextLine();
|
|
|
|
System.out.println("Enter your email: ");
|
|
String email = scanner.nextLine();
|
|
|
|
System.out.println("Enter your phone number: ");
|
|
Long phoneNumber = scanner.nextLong();
|
|
scanner.nextLine();
|
|
|
|
System.out.println("Enter your dateOfBirth: ");
|
|
String dateOfBirth = scanner.nextLine();
|
|
|
|
UserEntity user = null;
|
|
|
|
user = new UserEntity(firstName, lastName, nationalCode, email, phoneNumber, parseDate(dateOfBirth));
|
|
|
|
System.out.println(user.toString());
|
|
|
|
System.out.println(writeFile(user));
|
|
readFile();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String writeFile(UserEntity user) {
|
|
|
|
FileWriter fw = new FileWriter("userInformation.txt", true);
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
|
|
bw.write(user.toString());
|
|
bw.close();
|
|
return "user information written to file";
|
|
}
|
|
|
|
|
|
public static void readFile() {
|
|
|
|
FileReader file = new FileReader("userInformation.txt");
|
|
|
|
BufferedReader fileInput = new BufferedReader(file);
|
|
|
|
for (int counter = 0; counter < 3; counter++)
|
|
System.out.println(fileInput.readLine());
|
|
|
|
fileInput.close();
|
|
}
|
|
|
|
|
|
public static Date parseDate(String date) {
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
Date dob = null;
|
|
|
|
dob = formatter.parse(date);
|
|
|
|
|
|
return dob;
|
|
}
|
|
|
|
}
|