Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This was obviously the wrong forum to post in for timely replies...

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •