Can you write to a file from a vector??  
Author Message
nemadrias





PostPosted: 2006-8-2 3:53:00 Top

java-programmer, Can you write to a file from a vector?? How do I write to a file (using a FileWriter) from a filled vector??

I.E. Why can't I do this??

for (i = 0; i < myVector.size(); i++){
myWriter.write(myVector.elementAt(i));
myWriter.flush();
myWriter.close();
}

Thanks,
Steve

 
Oliver Wong





PostPosted: 2006-8-2 4:02:00 Top

java-programmer >> Can you write to a file from a vector??
"nemadrias" <email***@***.com> wrote in message
news:email***@***.com...
> How do I write to a file (using a FileWriter) from a filled vector??
>
> I.E. Why can't I do this??
>
> for (i = 0; i < myVector.size(); i++){
> myWriter.write(myVector.elementAt(i));
> myWriter.flush();
> myWriter.close();
> }

You (sometimes) can't write to a writer after you've closed it.

- Oliver

 
Matt Humphrey





PostPosted: 2006-8-2 4:07:00 Top

java-programmer >> Can you write to a file from a vector??
"nemadrias" <email***@***.com> wrote in message
news:email***@***.com...
> How do I write to a file (using a FileWriter) from a filled vector??
>
> I.E. Why can't I do this??
>
> for (i = 0; i < myVector.size(); i++){
> myWriter.write(myVector.elementAt(i));
> myWriter.flush();
> myWriter.close();
> }

First, because you're closing the file after the first element. Move the
close statement to outside of the loop. Second, because write writes out a
String (or char [] or int). You must convert whatever the object is at
myVector.elementAt (i) to a String. Minimally you can use toString (), or
if it's a vector of strings you can cast to String but otherwise it should
be something intellligible. What are you expecting it to output?

Matt Humphrey email***@***.com http://www.iviz.com/


 
 
nemadrias





PostPosted: 2006-8-2 4:13:00 Top

java-programmer >> Can you write to a file from a vector?? Thanks to both of you -
I don't know how I moved the .close() statement into the loop, but
somehow overlooked it. I did a toString() and that worked fine.
Thanks alot Matt,
Steve


Matt Humphrey wrote:
> "nemadrias" <email***@***.com> wrote in message
> news:email***@***.com...
> > How do I write to a file (using a FileWriter) from a filled vector??
> >
> > I.E. Why can't I do this??
> >
> > for (i = 0; i < myVector.size(); i++){
> > myWriter.write(myVector.elementAt(i));
> > myWriter.flush();
> > myWriter.close();
> > }
>
> First, because you're closing the file after the first element. Move the
> close statement to outside of the loop. Second, because write writes out a
> String (or char [] or int). You must convert whatever the object is at
> myVector.elementAt (i) to a String. Minimally you can use toString (), or
> if it's a vector of strings you can cast to String but otherwise it should
> be something intellligible. What are you expecting it to output?
>
> Matt Humphrey email***@***.com http://www.iviz.com/

 
 
Hendrik Maryns





PostPosted: 2006-8-2 21:47:00 Top

java-programmer >> Can you write to a file from a vector?? -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

nemadrias schreef:
> Thanks to both of you -
> I don't know how I moved the .close() statement into the loop, but
> somehow overlooked it. I did a toString() and that worked fine.

Please don鈥檛 top-post.

I鈥檇 move flush outside the loop too. No need to have that much I/O.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFE0KzLe+7xMGD3itQRAjD+AJ9TIYvF8qogVvh5z5mDQo4djVUDMwCeMc7m
UtCDjJeeS3TnF4bhKmvmz5Q=
=ivNG
-----END PGP SIGNATURE-----