Can you write to a file from a vector??

N

nemadrias

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
 
O

Oliver Wong

nemadrias said:
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
 
M

Matt Humphrey

nemadrias said:
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 (e-mail address removed) http://www.iviz.com/
 
N

nemadrias

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 said:
nemadrias said:
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 (e-mail address removed) http://www.iviz.com/
 
H

Hendrik Maryns

-----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’t top-post.

I’d 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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top