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 / May 2007

Tip: Looking for answers? Try searching our database.

Write to a file once a second

Thread view: 
Tinker - 17 May 2007 16:24 GMT
Hi,

I'm trying to write a single line to a file (say, myfile.txt) every second
using either javax.swing.Timer or java.util.Timer, but I'm struggling with
this.  Instead of posting my code, I was hoping someone might have a simple
example that writes to a file using a Java timer.

I have been using PrintWriter as the class to write to a file, which has
been perfectly fine, outside of the scope of the timer.  It's when the
writing occurs within the timer/action, that nothing gets written.

A simple example of how to do this is very welcome.

Many thanks,

Paul Tinker
Mike Schilling - 17 May 2007 18:49 GMT
> Hi,
>
[quoted text clipped - 6 lines]
> been perfectly fine, outside of the scope of the timer.  It's when the
> writing occurs within the timer/action, that nothing gets written.

You probably need to call flush() to see the changes appear in the file
promptly.
Tinker - 18 May 2007 13:16 GMT
Hi Mike,

Thanks for the advice - that got it.  I called flush() after the call to
println(<string>) and sure enough it gets written to the file.

It's odd, since I don't have to do this when the println is called outside
of the scope of the timer/action.  Any idea as to why?

Thanks again, cheers,
Paul

>> Hi,
>>
[quoted text clipped - 9 lines]
> You probably need to call flush() to see the changes appear in the file
> promptly.
Oliver Wong - 18 May 2007 16:07 GMT
> Hi Mike,
>
[quoted text clipped - 6 lines]
> Thanks again, cheers,
> Paul

   Perhaps the writer is being implicitly flushed when it closes due to
getting garbage collected when the program ends?

   Hard to say unless you post your code.

   - Oliver
Mike Schilling - 18 May 2007 17:37 GMT
> Hi Mike,
>
[quoted text clipped - 3 lines]
> It's odd, since I don't have to do this when the println is called outside
> of the scope of the timer/action.  Any idea as to why?

The buffer will flush when:

1. It fills up, or
2. The stream is explicitly flushed or closed, or
3. The program exits


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.