11 Replies - 2662 Views - Last Post: 10 May 2012 - 08:45 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 05:11 PM

I need to create a binary file to store the information in an arraylist, i think i already have that done. (Cant tell because i cant read a binary file)

Now i need to take the data from the binary file and load it back into the arraylist when the program starts up. Can someone explain how i would go about that?


Also is it possible to have the arraylist save to the binary file only when the program closes, regardless of how it is closed? So for example if i click the X button in the top right it will save the list to the binary file, if i click Alt + F4 it will save it, etc. I think that would be easier than trying to save it every time anything changes to one of the pieces of data in the arraylist.

Is This A Good Question/Topic? 0
  • +

Replies To: How do you populate an arraylist from a binary file?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 06:58 PM

My post from your previous thread links to a tutorial on reading in Serialized objects.

If you want to perform actions when your Window is closed, you can us a WindowListener. There is no way with non-GUI programs to enforce this though (and really, the user can quit your GUI program to circumvent this as well).
Was This Post Helpful? 1
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:17 PM

Thanks, but i am still having problems with it. For example when i tried to load the file all it did was output this: SavingsAccount@5d3e754f

This is the code i am using to create the file using the arraylist and to try and put the file into the arraylist when the program starts up.

public void createFile() {
                try {
        // Create file object for new file we are about to create
        File outFile = new File("accounts.dat");
        
        // Construct the stream objects to do the work
        FileOutputStream outFileStream = new FileOutputStream(outFile);
        ObjectOutputStream outDataStream = new ObjectOutputStream(outFileStream);

        
        // Write the object into the binary file as it currently is
        outDataStream.writeObject(accounts);
        
        // Close the stream
        outDataStream.close();
        
    } catch (Exception e) {
            System.out.println("Binary file input error: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public void loadFile() {
        try {
            // Create file object of file to read
            File inFile = new File("accounts.dat");
            // Does it exist?
            if (inFile.exists()) {
                // Construct the stream objects to do the work
                FileInputStream inFileStream = new FileInputStream(inFile);
                ObjectInputStream inDataStream = new ObjectInputStream(inFileStream);
                
                accounts = (ArrayList<Accounts>) inDataStream.readObject();
                inDataStream.close();
                
                for (Object s:accounts) {
                    System.out.println(s);
                }

            }
                                       
        } catch (Exception e) {
            System.out.println("Binary file input error: " + e.getMessage());
            e.printStackTrace();
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:20 PM

Since your Account class does not override the Object toString() method, the memory locations are being printed. You need to override the toString() method in your Account class definition so that actual, human significant Strings are being outputted.
Was This Post Helpful? 2
  • +
  • -

#5 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:29 PM

Uhh it's late and i havent been getting much sleep lately so unfortunately i dont really understand what your saying, what toString() method?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8010
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:41 PM

	                for (Object s:accounts) {
	                    System.out.println(s);


When you call print() on an object, what is printed is the result of calling that object's toString() method. The toString method is inherited from the Object class, and if you don't override it, it'll use that default method, which simply prints out the name of the class and unique identifier for the particular instance you're handling at this particular moment. That unique identifier happens to be the object's address in memory, but that means nothing to you, since you can do nothing with that information.

If you want to have the quoted lines make sense, you have to modify your account class to return something useful when you call its toString method.
Was This Post Helpful? 1
  • +
  • -

#7 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:51 PM

Hmm guess it may be putting things into the array properly but just not able to view it that way, cause using accounts.get().datahere works to bring up that value from the index of the array i specify.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8010
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 07:53 PM

Yes, it's obvious that you have objects because you're able to see them. You couldn't get that SavingsAccount@5d3e754f if there weren't a SavingsAccount in that ArrayList.
Was This Post Helpful? 1
  • +
  • -

#9 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 08:07 PM

Alright, so about getting it to save the arraylist to a file when the program window is closed, there is really no way to make it so that if they close the window in any way it automatically saves the file? That would be so much simpler than going through and making it save when any change is made.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8010
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 08:21 PM

You can't be sure that the program will exit gracefully every time, so you don't have control over whether the save will always execute before the program shuts down. For example, if someone trips over the power cord and de-boots the machine, your data is lost. For that reason, I'd suggest that periodic saves would be a nice feature.

A WindowListener can detect when the user chooses to close the window, as you've already been told, and by intercepting this event, you can perform any cleanup that you deem necessary before exiting, including saving the user's work to a file.
Was This Post Helpful? 1
  • +
  • -

#11 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 08:40 PM

Oh well guess i have to go with that.

Now one other thing, i have a text field thats set up to only accept numbers using \\d+ is there a way i can modify that so it will accept a decimal in there as well? Like currently if i try putting 123.45 in it would pop up with the error i put in because it counts the . key as not a number.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8010
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: How do you populate an arraylist from a binary file?

Posted 10 May 2012 - 08:45 PM

Get the contents of the textfield and try to parse it as a double. If the parseDouble throws an exception, use that as an opportunity to warn the user that their input was not good and return them to an empty textfield.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1