Java 2.4 Nites
Java 2.4 Nites
1. Aim: Create a menu based Java application with the following options.1. Add an
Employee2.Display All3.Exit If option 1 is selected, the application should gather details of
the employee like employee name, employee id, designation and salary and store it in a file. If
option 2 is selected, the application should display all the employee details. If option 3 is
selected the application should exit.
2. Objective:
• To learn about concept of File Handling in java.
import java.io.*;
import java.util.*;
while (true) {
System.out.println("\nMain Menu");
System.out.println("1. Add an Employee");
System.out.println("2. Display All");
System.out.println("3. Exit");
@SuppressWarnings("unchecked") private
static void loadDataFromFile() { File file
= new File(FILE_NAME);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(FILE_NAME))) {
employees.addAll((List<Employee>) ois.readObject());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
Output:-