Two Laptop Bag*
The moose likes Beginning Java and the fly likes writing to file from Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing to file from Vector" Watch "writing to file from Vector" New topic
Author

writing to file from Vector

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I am trying to write data to a file but it is not working. I've tried using the file account.txt and account.dat with no difference. All I want to do is write the data to a file then read it back. Here is what I have:

I can get gan and vector size but I get [AccountTransaction@12a55aa]
for transaction and my file is empty.
Help!!
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
What does the exception that's probably being generated say?
Is transaction a Vector or an AccountTransaction? What should it be?
Is AccountTransaction serializable?
The file extention doesn't matter, ie: if you put .bin it doesn't make it binary, you make it binary with the information you put in it. File extentions are just a way to say "this file contains text", or whatever. An OS can use extentions to find the program to open that type of file.
[ January 28, 2004: Message edited by: Ray Stojonic ]
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Candy Bortniker:


That's very likely at least one of your troubles. That should be:
if (e.getActionCommand().equals("Done"))
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
What does the exception that's probably being generated say?
I don't get an exception, I just don't get anything in the file.
Is transaction a Vector or an AccountTransaction? What should it be?
transaction is the Vector and I think that is what it should be.
Is AccountTransaction serializable?
Yes, it is serializable.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Billy,
You certainly raise a good point, but...while it's much better form to use equals(), == will still work because setActionCommand( "Done" ) (which we don't see here, but it must have been set) would put the 'Done' String literal into the String pool, which would be the same String object as in getActionCommand() == "Done"
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326

Is transaction a Vector or an AccountTransaction? What should it be?
transaction is the Vector and I think that is what it should be.

if:
System.out.println ("Data is vector is: " + transaction);
results in:
Data is vector is: [AccountTransaction@12a55aa]
then transaction is an AccountTransaction
Are you printing your catch block? ie:
catch( IOException ioe ) { System.out.println( ioe ) }
if not, you don't know if you'e getting an exception or not
prove me wrong, put a System.out after the .writeObject() and see if it prints
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Ray Stojonic:
Billy,
You certainly raise a good point, but...while it's much better form to use equals(), == will still work because setActionCommand( "Done" ) (which we don't see here, but it must have been set) would put the 'Done' String literal into the String pool, which would be the same String object as in getActionCommand() == "Done"

Only "better form"? I'd say it's essential to use .equals regardless, as the == form will only work if in fact it was coded the way you said. If the implementation were to change such that for example it read the "Done" string from a resource instead of hard-coding it, the code would certainly break if it continued using the == form.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Billy,
Make no mistake, I agree with you.
to me: better form is always essential
for the scope of this thread, == works, that's all I was saying.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123

Are you printing your catch block? ie:
catch( IOException ioe ) { System.out.println( ioe ) }
if not, you don't know if you'e getting an exception or not
prove me wrong, put a System.out after the .writeObject() and see if it prints

--------------------------------------------------------------------------------

I do have a catch. It looks like:

I did change to (e.getActionCommand().equals("Done") but that didn't change anything.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
if:
System.out.println ("Data is vector is: " + transaction);
results in:
Data is vector is: [AccountTransaction@12a55aa]
then transaction is an AccountTransaction
Here is where I define transaction:
private Vector transaction; //vector to hold account information
AccountTransaction trans = new AccountTransaction();
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Candy Bortniker:
ObjectOutputStream oos = new ObjectOutputStream (
new FileOutputStream( "account.dat" ) );

Here's likely another trouble spot. Do you realize that if it executes that piece of code, it will create a file named "account.dat" in the current directory (which may not necessarily be where you think it is)?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
You can certainly try inserting the flush(), but in reality you needn't worry about that, as that will be taken care of by close().
[ January 28, 2004: Message edited by: Billy Bobbins ]
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
My previous reply was in reference to another reply which had suggested inserting a flush() call prior to close(). (But now that reply is missing - editing/deleting replies is kind of nice, but it's also messy as other replies are then rendered outdated)
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Here's likely another trouble spot. Do you realize that if it executes that piece of code, it will create a file named "account.dat" in the current directory (which may not necessarily be where you think it is)?

--------------------------------------------------------------------------------
The file goes to the same directory that the program is in which is fine. The file gets created there is just nothing in it.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Candy Bortniker:
The file goes to the same directory that the program is in which is fine. The file gets created there is just nothing in it.

Does the timestamp of that empty file update to reflect the time you ran it? If not, I'd say you're not looking at the file the program is actually writing to. If so, then I'd go ahead and try a oos.flush() before the oos.close() just to rule that out (though it should not be necessary).
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I checked the timestamp and found out I was not writing to the file I thought I was. However, the file I am writing to is not getting the correct data it is getting stuff like:
�� sr java.util.Vector��}[�;� I capacityIncrementI elementCount[ elementDatat [Ljav
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
That's what you should be getting, you're writing an Object after all. The idea it is to put an Object into a file in it's current state, then read it back as the Object. This forgoes the need to convert it to text then reads the text back and load up an Object.
If you want human readable text in the file, use a PrintWriter and extend Vector and override toString() to do write the contents of the Vector (though that doesn't sound like what you wanted in the original post)
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Great, then I do actually know what I'm doing. Thanks for all the help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: writing to file from Vector