We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › writing data to file
Page Index Toggle Pages: 1
writing data to file (Read 520 times)
writing data to file
Feb 21st, 2010, 10:19pm
 
i am writing a programm that logs something over a longer period of time. at least some hours or even a day.
when using printwriter the data is sometimes lost when i close the program from the IDE. it seems like i have to use flush() and close()
is there another way to continously write to a file without the possibiltie of losing the data when the program quits.

or can i just use flush() every frame ?
if i still use the printwiter. does it collect all the data in memory before its written to file? cause wouldnt that mean it could be too much when running for a long time ?

Re: writing data to file
Reply #1 - Feb 21st, 2010, 11:03pm
 
You guessed, just use flush().
To answer your last question, yes, data is bufferized in memory between flush calls (but I bet that when the buffer is filled, it is flushed automatically). That is to reduce disk accesses, which are slow. That's the classical dilemma between speed and security.
Note: you can also use the stop method to close properly your file.
Re: writing data to file
Reply #2 - Feb 21st, 2010, 11:12pm
 
alright, good to know. i just wanted to be sure that it doesnt crash when not using flush.
so maybe i just flush it once every minute to be sure to save it ,not to lose any data.
Thanks!
Re: writing data to file
Reply #3 - Feb 21st, 2010, 11:15pm
 
as you suggested it gets flushed when the buffer is full. i guess. so thats probably the reason why the files where i logged only less data werent saved, but the ones where i logged a lot of data always got saved. i guess because they were flushed by themself...
thanks again.
Page Index Toggle Pages: 1