• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Problem

 
Kevin Green-Hawkings
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered using RandomAccessFile?
 
Kevin Green-Hawkings
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RandomAccess does primitive methods, not objects. It doesn't have a ReadObject or WriteObject method.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I have so far:



 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic