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 › Append to an Existing Line in text file
Page Index Toggle Pages: 1
Append to an Existing Line in text file (Read 957 times)
Append to an Existing Line in text file
May 11th, 2009, 8:43am
 
Hello,
I am trying to append data to existing lines in a text file, I need to specify which line to write too, but I don't see this information in the reference.

For example if the text file looks like this:

line1:
line2:

I would like to be able to write specific data to each line, something like:

writer.appendLines(1, "this is line 1");
writer.appendLines(2, "this is line 2");

text:
line1: this is line 1
line2: this is line 2


Thanks in advance for any help
Re: Append to an Existing Line in text file
Reply #1 - May 11th, 2009, 9:11am
 
At least two ways to do it:
- If file isn't very big, just read it into an array of lines, append what you need, and write back the file;
- If the file is really big, read it line per line, writing them to another file, and when reaching the target lines, modify them before writing them.
Re: Append to an Existing Line in text file
Reply #2 - May 11th, 2009, 1:34pm
 
Code:

BufferedWriter writer;

writer = new BufferedWriter(new FileWriter("filename", true));
writer.write("yourString\r\n");
writer.close();


That will append to the end of a file but if you want to be able to use something like:

"writer.appendLines(1, "this is line 1");"

you would have to write a library/class for that purpose using the method PhiLho described.
Page Index Toggle Pages: 1