Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 11-12-2012, 03:17 PM   PM User | #2
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
This was obviously the wrong forum to post in for timely replies...
OldBlue is offline   Reply With Quote
Old 11-12-2012, 04:59 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by OldBlue View Post
This was obviously the wrong forum to post in for timely replies...
Its a long weekend, you can't expect very many people are around :P
FileWriter has five constructors to it:
Code:
public FileWriter(File);
public FileWriter(File, boolean);
public FileWriter(FileDescriptor);
public FileWriter(String);
public FileWriter(String, boolean);
So with the exception of FileDescriptor, providing a boolean as a second argument will flag appending.
An alternative is to write the data files as serialized. The collection is already serialized, so just implement Serializable on the object class within it, and you can just ovewrite the file every time. Useful if things are small, typically for something like configurations, but you never need to worry about appending since it'll always overwrite the entire file (and therefore can accommodate a changing list as well).
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
arraylist, file, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:26 AM.


Advertisement
Log in to turn off these ads.