You have to write this behavior into the program yourself.
From what I see from your description, perhaps the thing for [u]you</u> to do (given that you write to a file) is to keep track of how many lines you have written to the file. If they click “Back” or “Undo,” create a new file, and write all of the lines of the current file into the new file--except the most recent. If you just wrote line 5, write 4 lines to the new file. If you just wrote line 28, write the 1st 27 lines of the current file to the new file.
Then close both files, delete the original, rename the new one, and open it as the currnet file. Then present the previous display to the user to allow them to make the right pick this time.
The thing for you to do will be dictated by what your program does, how it implements tha, and so on. But the bottom line is that you have to write “Undo” behavior yourself. (With a database you can make this easier with transactions, but you still have to write the code that uses transactions, and their ability to “Rollback” to the start of the transaction.)