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 › add line to txt file
Page Index Toggle Pages: 1
add line to txt file (Read 1781 times)
add line to txt file
Aug 23rd, 2009, 4:06am
 
Hi, just a short Syntax question,
i have an existing txt file where i store different values each one in a different line, i read them with loadstring in the setup...

during the runtime of the programm, i want to add some lines so i can read them next time the programm is started.
What i need to know is, how to add another line to a not empty txt file.

Normally i do that with the printwriter or saveStrings(), but it erases the already written lines. How can i just add another line of data to it?

Thx!

edit:
hmm just thought about it, i can write everything again and add the last line.... but if there is a better way, let me know

Re: add line to txt file
Reply #1 - Aug 23rd, 2009, 4:32am
 
Indeed you can rewrite the full data, but it is expensive...
The classical way (there are several others!) seems to be:
Code:
BufferedWriter bw = null;
try
{
FileWriter fw = new FileWriter(outFileName, true); // true means: "append"
bw = new BufferedWriter(fw);
bw.write(newData + "\n");
}
catch (IOException e)
{
// Report problem or handle it
}
finally
{
if (bw != null)
{
try { bw.close(); } catch (IOException e) {}
}
}
Re: add line to txt file
Reply #2 - Aug 24th, 2009, 7:24pm
 
Thx PhiLho, but i just cant make it work.

no matter what i do, it doesnt write to the file. I also tried this piece of code i found on the board : http://processing.org/discourse/yabb/YaBB.cgi?board=Integrate;action=display;num=1113338206
it also doesnt work.
Shouldnt your piece of code work if I put only it in the sketch and execute it ?
Re: add line to txt file
Reply #3 - Aug 24th, 2009, 11:23pm
 
Yes, I tested it before posting... Smiley
Obviously, you must provide the outFileName (I used an absolute path IIRC) and the newData (a line of text).
Perhaps the problem was with the file path (I was misleading in the choice of name), as it is Java API, without notion of sketch folder, so if you provide only a name, it will write in its current directory, which is probably where the .class files are (in temp dir...).
Re: add line to txt file
Reply #4 - Aug 25th, 2009, 2:16am
 
sure i added a file name and some kind of date Smiley
but it was the whole path i had to add... .

Its working right now, but it only adds it to the end of the file but but not to a new line. I know it should because if "\n" but it just dont do it. Any ideas  in this case ? Roll Eyes
Re: add line to txt file
Reply #5 - Aug 25th, 2009, 2:43am
 
ok, i used System.getProperty("line.separator"); instead. seems to be the better choise anyway.
Page Index Toggle Pages: 1