aspose file tools*
The moose likes Beginning Java and the fly likes Array Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Problem" Watch "Array Problem" New topic
Author

Array Problem

Kevin Green-Hawkings
Greenhorn

Joined: Apr 24, 2012
Posts: 5
Good morning!

I have an array of animal objects. I want to place the said objects into a file called farm.dat. Utilizing FileOutputStream and ObjectOutputStream, I can place the animals into the file with no problem.

Then using "tempFarm" object, I can pull the animals back out and print them, display them, eat them, etc.

How do I put them "back" into the farm.dat so that if I run a seperate program that just reads the farm.dat file, any changes I've made to objects in my main program are saved?
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

Have you considered using RandomAccessFile?
Kevin Green-Hawkings
Greenhorn

Joined: Apr 24, 2012
Posts: 5
RandomAccess does primitive methods, not objects. It doesn't have a ReadObject or WriteObject method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kevin Green-Hawkings wrote:
How do I put them "back" into the farm.dat so that if I run a seperate program that just reads the farm.dat file, any changes I've made to objects in my main program are saved?


Just write everything out after your changes the same way you wrote it out before your changes.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Panagiotis Kalogeropoulos wrote:Have you considered using RandomAccessFile?


No, that doesn't seem appropriate here. Nothing he's said so far suggests he wants to jump to specific byte locations in the file.
Kevin Green-Hawkings
Greenhorn

Joined: Apr 24, 2012
Posts: 5
Here is what I have so far:



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Kevin Green-Hawkings wrote:Do I write the animals array again... or the tempFarm array?


Well, that depends. Do you want to write the new data? Or rewrite the old data?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kevin Green-Hawkings wrote:Do I write the animals array again... or the tempFarm array?


You write out the latest version of the same structure you initially wrote out.

[code]
1. Create X[].

2. Populate X[].

3. Write X[] to file.

4. Read from file into X[].

5. Modify X[].

6. Write the modified version of X[] out to the file, replace the X[] what was there before.

I'm really not sure what trouble you're having. Did you actually try writing it back out and encountered a problem? Or are you just unsure of what you need to do?

If the former, post your code, or an SSCCE, and describe the problem in detail, including pasting in the exact error message, if any.

If the latter, then just try it based on what you think you understand, and if there's a problem and you can't figure it out, post again.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Jeff Verdegan wrote:
Panagiotis Kalogeropoulos wrote:Have you considered using RandomAccessFile?


No, that doesn't seem appropriate here. Nothing he's said so far suggests he wants to jump to specific byte locations in the file.



BTW, even if it was appropriate, I am not sure if it is possible. It will only work if each element of the array are the same size.

Henry
Kevin Green-Hawkings
Greenhorn

Joined: Apr 24, 2012
Posts: 5
So something like this? I guess I'm having problems with the last two steps...

5. Modify X[].

6. Write the modified version of X[] out to the file, replace the X[] what was there before.


Thanks for your help!

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kevin Green-Hawkings wrote:So something like this?


Well, does that produce the results you wanted? For instance, if you then re-read the file after that and print out what you read, is it what you expect?

A couple of notes:

1) You forgot to close() in at least one csae.

2) Calling println(X) is the same as calling println(X.toString()), except that if X is null, the first will print "null" and the second will give NullPointerException.

3) If you change your Animal toString() method to return the type in addition to whatever it's already returning, you won't have to print the animal and its type separately. This is up to you though; it depends on what you want Animal's toString() to do.
Kevin Green-Hawkings
Greenhorn

Joined: Apr 24, 2012
Posts: 5
It does... I'm just a little puzzled. If I drag the objects out of the file back into the array, play with them, and then put them back into the object as the same array (re-writing it), and then pull them back again into the program, the changes show. However, when I terminate the program, and use another short program just to read the farm.dat file, the changes aren't saved? That is where I'm stumped.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Kevin Green-Hawkings wrote:It does... I'm just a little puzzled. If I drag the objects out of the file back into the array, play with them, and then put them back into the object as the same array (re-writing it), and then pull them back again into the program, the changes show. However, when I terminate the program, and use another short program just to read the farm.dat file, the changes aren't saved? That is where I'm stumped.



It looks like what you doing is....

1. Open a file for writing.
2. Write out the array.... but don't close the file.

then....

3. Open the already previously opened file for reading.
4. Read in the data and then change the value.

then...

5. Write the changed data to the output file.


Two issues with this... First, since you never closed and reopened the output file, all you are doing is appending. Basically, writing the same array twice. Second, there are some optimization code in output stream that may affect you when you write the same object twice. I would speculate that your second program only read one of the two written objects from the file.

Henry

 
jQuery in Action, 2nd edition
 
subject: Array Problem
 
Similar Threads
Object types in an array
listing files in a sorted order
Regarding the FINAL qualifier and arrays...
Polymorphism
Trouble with getting input from keyboard