naved momin wrote:
Stephan van Hulst wrote:Copy the data starting at line 3, then go to line 2, write what you want to write, and then append everything you copied.
If you have to copy a *lot*, you can temporarily store the data in a new file, and then merge the two files at the correct position once you're done making edits.
but thats the whole point of asking this question because programatically how i can just copy the 3rd line ?
I'm not sure what he meant by that.
I would suggest one of 2 approaches.
1. If the file is small enough, read line by line into a List in memory. Then write out to replace the original file. After you've written the first 2 lines, insert the new line, then write the rest of the lines from the list. (Or you could insert the new line into the list and then just iterate over the list, writing out each line.)
2. For a larger file, create a temp file. Read a line from the original, write it immediately to the temp, so that you have only one line at a time in memory. When you've read all the lines up to the point you want to insert something, write out that new line. Then continue the read a line/write a line loop. When you're done, delete the original and rename the new one to the original name.