I need help with a fairly simple student info management program that I am working on. It contains a GUI with two fields for collecting data: name and ID# (which are stored in an ArrayList). I can presently add data and query for existing data, but I'd like to implement a function that loads data from a text file in the ArrayList and also a function to save additional data into the same text file.
I've found examples of code to save the data to the text file, but when I test it, the text file is cleared out instead of being appended as it should. Can anyone tell me 1) how to load the text file into the ArrayList, and 2) how to save (append) the data back into the text file?
Here is my code from the method that handles button functions:
Code:
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equalsIgnoreCase("Add")){
this.addStudentRecord();
}
if(e.getActionCommand().equalsIgnoreCase("Query")){
this.queryStudentRecord();
}
if(e.getActionCommand().equalsIgnoreCase("Exit")){
System.exit(0);
}
if(e.getActionCommand().equalsIgnoreCase("Load")){
}
if(e.getActionCommand().equalsIgnoreCase("Save")){
try {
FileWriter out = new FileWriter("studentrecord.txt");
BufferedWriter writeFile = new BufferedWriter(out);
for(int i=0; i<stuRecordStore.size();i++){
writeFile.write(stuRecordStore.get(i).getStudentName());
writeFile.newLine();
writeFile.write(stuRecordStore.get(i).getStudentID());
writeFile.newLine();
}
writeFile.close();
out.close();
queryResultTextArea.setText("Data written to file.");
}catch (IOException e1) {
e1.printStackTrace();
}
}
ANY help is GREATLY appreciated.