0

I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.

for example, i have a line in my text file which is:

hello my name is

I would like to add to this line so it becomes:

hello my name is joe bloggs

Thanks

i have a task to create a help desk program and i am trying to incorporate a feature that enables users to edit questions they have posted. as a result, the program will need to be able to append Any line within the text file - not necessarily just the last line

kdav4
  • 39
  • 2
  • 2
  • 5
  • http://stackoverflow.com/questions/822150/modify-a-txt-file-in-java – Jeremy D Mar 01 '12 at 16:46
  • 1
    Read the entire I/O trail: http://docs.oracle.com/javase/tutorial/essential/io/ and specifically http://docs.oracle.com/javase/tutorial/essential/io/rafs.html to learn I/O in Java. – Matthijs Bierman Mar 01 '12 at 16:51

1 Answers1

3

If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.

The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194