CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Help with loading and appending a txt file using an ArrayList (http://www.codingforums.com/showthread.php?t=281918)

OldBlue 11-12-2012 12:21 AM

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 11-12-2012 03:17 PM

This was obviously the wrong forum to post in for timely replies...:(

Fou-Lu 11-12-2012 04:59 PM

Quote:

Originally Posted by OldBlue (Post 1291254)
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).


All times are GMT +1. The time now is 12:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.