aspose file tools*
The moose likes Beginning Java and the fly likes Still can't write vector to file...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Still can Watch "Still can New topic
Author

Still can't write vector to file...?

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I want to write a static method in my program called writeFile(). I have a class hierarchy in place which has an abstract class Threat, classes that extend it, an abstract class Malware which also extends Threat (and classes that extend Malware). They are all diffrent threats that I create in the program. I make the Class at the top of the hiearchy (Threat) implement Serializable because every other class extends it. Then i create a vector which stores all these threat objects I create. I thought it was possible to write the entire vector to a txt file with one command? Don't worry, i have try and catch blocks in place, and I do the same for closing the file. How do I write a vector with objects to a txt file then?

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
Did you run it? What is the error?
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
There is no error, it compiles...although i do get a warning, "java uses unchecked or unsafe operations"...but I had that before. I think it's because I have a factory method. I use it to create objects according to input by the user. Anyways, the file I try to write to isn't even created after i run the program, and If i create a blank txt file and name it accordingly, nothing gets written to it. I'm stumped.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
nevermind...i'm just a bit slow. I didn't realise that I commented out the call to the static method which passed the vector as a parameter. I was making some mods to the program and completely forgot i did that. It wasn't writing aything to file because I didn't pass the method the vector. OOPS!! got it working now, sorry to waste your time.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Originally posted by John Lockheart:
There is no error, it compiles...although i do get a warning, "java uses unchecked or unsafe operations"...but I had that before. I think it's because I have a factory method. I use it to create objects according to input by the user. Anyways, the file I try to write to isn't even created after i run the program, and If i create a blank txt file and name it accordingly, nothing gets written to it. I'm stumped.


Works for me... created test vector, with a bunch of strings, wrote the vector out to the object output stream, closed the file, open the file for reading, read the vector back via object input stream, printed it out to confirm it. The file is definitely created, and the data is stored correctly, as I was able to read it back in.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Still can't write vector to file...?