View Single Post
Old 11-12-2012, 12:21 AM   PM User | #1
OldBlue
New to the CF scene

 
Join Date: Oct 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
OldBlue is an unknown quantity at this point
Help with loading and appending a txt file using an ArrayList

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.
OldBlue is offline   Reply With Quote