Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Can you write to a file from a vector??

Thread view: 
nemadrias - 01 Aug 2006 20:53 GMT
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 - 01 Aug 2006 21:01 GMT
> How do I write to a file (using a FileWriter) from a filled vector??
>
[quoted text clipped - 5 lines]
>         myWriter.close();
> }

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

   - Oliver
Matt Humphrey - 01 Aug 2006 21:07 GMT
> How do I write to a file (using a FileWriter) from a filled vector??
>
[quoted text clipped - 5 lines]
>         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 matth@ivizNOSPAM.com http://www.iviz.com/
nemadrias - 01 Aug 2006 21:13 GMT
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

> > How do I write to a file (using a FileWriter) from a filled vector??
> >
[quoted text clipped - 14 lines]
>
> Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Hendrik Maryns - 02 Aug 2006 14:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.