Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Doing exam prep for college (Java OOP) and am really stumped on something. Any help would be appreciated. During the year I missed most of the I.O section (major mistake). Anyway, I shall try explain my problem best I can.

My application uses a swing UI and users should be able to create a bank account with a name, number and balance (String int and double). I am using an Account class to set, get and store variables.

I need functionality to add a new account, view all accounts, and then store all customer details in a file and be able to read them after the application is re-opened. Im using an ArrayList to store ArrayList<Account> accList; and here is what I've come up with so far for writing the objects to file (I have the relevant FileHandling code already in so I'm not going to include the catch statements) :

        Account account = new Account(name, number, balance);
        accList.add(account);

        try {
        ObjectOutputStream out;
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(output)));
        for (int i = 0; i < accList.size(); i++) {
            out.writeObject(accList.get(i));
        }
        out.close();

    }

Seems to work fine.. its the viewing from file I seem to have a problem with. Here is a code segment:

 ObjectInputStream in = null;

    Account account;

    try {
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(output)));

        while (true) {
            account = (Account) in.readObject();
            accList.add(account);
            jTextArea1.setText(accList.toString());
        }

The toString() method is my own @Override method in the Account class and prints the names fine. The poblem occurs when I press "view accounts" twice. It re-prints the output viewed everytime, so if there was 1 Account when first viewed, the next time it would be 2, then 3 etc. I know for such a long-winded question theres probably a simple answer but I have been looking at it for ages now and need another perspective!

share|improve this question
2  
You're making your life hard. Instead of looping to write and read every acount, why don't you write the list itself (only one call to ObjectOutputStream.writeObject()), and read the list itself (only one call to ObjectInputStream.readObject())? –  JB Nizet May 10 at 1:35
    
Yeah as I said I am only learning and missed most of File IO classes so I've had to learn most of it myself and obviously missed a few crucial parts. Are you saying I can write the whole arrayList with writeObject? –  Adam May 10 at 1:38
1  
Yes. ArrayList is Serializable, so you can serialize it with writeObject(). –  JB Nizet May 10 at 1:39
    
Thanks haha i knew it would be REALLY simpler than what I was doing. It is working now :) –  Adam May 10 at 1:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.