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 › Continuosly write in a txt file
Page Index Toggle Pages: 1
Continuosly write in a txt file (Read 1275 times)
Continuosly write in a txt file
Jan 16th, 2010, 7:02pm
 
Hello!
~I would like to know if there is a way of processing continously writing in a txt file even after you close the program.. i am tryin to make a program that saves the name, present date and time of the user and would like to store it for later use.
The only examples i could find would overwrite what was previously stored.
Is there a simple way of doing this?

Any help would br greatly apreceated!
Re: Continuosly write in a txt file
Reply #1 - Jan 16th, 2010, 7:44pm
 
maybe there are better ways, but this works:

Quote:
void writeLog()
{
     
     String newLine = "the app was launched on "+year()+"/"+month()+"/"+day()+" at "+hour()+":"+minute()+":"+second();
     String logLines[] = loadStrings("log.txt");
     if(logLines != null)
     {
           logLines = append(logLines, newLine);
     }
     else
     {
           logLines = new String[1];
           logLines[0] = newLine;
     }
     saveStrings("log.txt", logLines);
}


-try to load Strings from a file "log.txt" from the sketch/data folder
-if data exists, append the new line containing date etc..
-if not, make a fresh String[]
-overwrite the old file (or create a new one)
Re: Continuosly write in a txt file
Reply #2 - Jan 17th, 2010, 1:20am
 
just use the search, there have been several posts about this topic
http://processing.org/discourse/yabb2/?num=1262894607
Re: Continuosly write in a txt file
Reply #3 - Jan 17th, 2010, 2:48am
 
phling's way can work but might be overkill if the log grows large! (some logs can be several MB).
Cedric pointed to threads where we show how to open a file in append mode.
But if you have serious needs, you might want to look at a real logger, like log4j, optimized for this usage, able to change log file when the last one is too big, etc.
Re: Continuosly write in a txt file
Reply #4 - Jan 17th, 2010, 12:15pm
 
Thank you so much for the response from all of you! You have been all great help. Sorry for the late response but my internet got cut for a while.

phling thank you so much for the code, it works perfectly. You were really a great help.
Cedric i did the search and found that exact topic and for some reason i thought the topic creator was asking something else... sorry it was a misunderstanding.
PhiLho thx for the heads up, but the log file wont be ued to many times so i dont think it will get affected.. but thx for directing this to my attention. If i ever need it larger i will use your post as reference.

thank you again for all your help!
Page Index Toggle Pages: 1