I am creating a program that keeps track of employees. I have an ArrayList that keeps track of the current employees. Since ArrayLists aren't preserved after the program restarts I needed to find a way to preserve the employee list. I decided to create a file that had a list of all the employees then every time the program ran it would import the file into an ArrayList. I created a save method and an addEmployee method and this is what I have so far:
Code to create new employee (not addEmployee method):
System.out.println("Please enter the new employee's first name:");
firstName = addEmployee.nextLine();
System.out.println("Please enter the new employee's last name");
lastName = addEmployee.nextLine();
addEmployee(firstName, lastName); //calling the addEmployee method
save("employeetest"); //saving the array
Save method:
public static void save(String fileName) throws FileNotFoundException {
String tmp = employees.toString();
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName,true));
pw.write(tmp);
pw.close();
}
The problem is every time you create a new employee it saves the entire ArrayList again, not just the employees name. So I decided to load just the name of the employee (as a string) at the end of the file. But now I need to load the file of Strings into an ArrayList. How should I convert the Strings from the file into an ArrayList and then load it into the ArrayList?