kdav4
kdav4

Reputation: 39

How to append existing line within a java text file

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

Upvotes: 0

Views: 529

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503280

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.

Upvotes: 3

Related Questions