Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Can you write to a file from a vector??

Reply
Thread Tools

Can you write to a file from a vector??

 
 
nemadrias
Guest
Posts: n/a
 
      08-01-2006
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

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      08-01-2006

"nemadrias" <> wrote in message
news: oups.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

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      08-01-2006

"nemadrias" <> wrote in message
news: oups.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 http://www.iviz.com/


 
Reply With Quote
 
nemadrias
Guest
Posts: n/a
 
      08-01-2006
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" <> wrote in message
> news: oups.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 http://www.iviz.com/


 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      08-02-2006
-----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+AJ9TIYvF8qogVvh5z5mDQo 4djVUDMwCeMc7m
UtCDjJeeS3TnF4bhKmvmz5Q=
=ivNG
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I have no idea, you didn't say what country state or city you're in and you can't even write a proper post anyways. richard Computer Support 7 08-01-2010 09:12 PM
How do you write JavaScript in document.write and have it workproperly? Frank Peterson Javascript 9 06-28-2009 03:43 AM
Re: It seems that ZipFile().write() can only write files,how can empty directories be put into it? could ildg Python 0 07-02-2005 08:31 AM
Re: It seems that ZipFile().write() can only write files,how can empty directories be put into it? Jeff Epler Python 0 07-01-2005 02:06 PM
It seems that ZipFile().write() can only write files,how can empty directories be put into it? could ildg Python 0 07-01-2005 01:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57