1

I've looked around for quite a bit on this, but I haven't seen examples on how to do this. I apologize if this has been answered before, I was unable to find it.

I know the basic idea behind the concept: Make a temp text file, copy the text before the line you want to edit to the temp text file, make the edit to the line in the new file, copy the rest to it, overwrite the old file, and delete the temporary file. However, I haven't seen any examples on how to do this. If this is incredibly simple, I am really sorry, I'm kinda learning as I go here.

Makaires
  • 107
  • 1
  • 8
  • 5
    1-, Why are you looking around for code to do this? You have outlined the steps you need to do, so just do it! If you have problems then post a specific question about a specific step and post your [SSCCE](http://sscce.org/) that demonstrates the problem so we can see what you have done so far. – camickr Jan 17 '15 at 05:00
  • While I know the individual code for each part, I'm really having a lot of trouble putting it all together, both mentally and the actual coding. I'm not wanting the actual code to copy and paste in, just an... example, I guess. – Makaires Jan 17 '15 at 05:02
  • Maybe look at some examples? http://stackoverflow.com/questions/17218971/i-want-open-a-text-file-and-edit-a-specific-line-in-java – candied_orange Jan 17 '15 at 05:06
  • 1
    You didn't event take the time to read the `SSCCE` link I provide you. If you already have the code then I don't see the problem. Post your `SSCCE` that shows how you are trying to put the code together. Do step1, then step 2 , then step 3 until you finish – camickr Jan 17 '15 at 05:06
  • @CandiedOrange Thank you for that. I searched the title of that post nearly verbatim and it didn't show up. – Makaires Jan 17 '15 at 05:08
  • You're welcome. I just typed "java edit text file line by line" in google. Also http://stackoverflow.com/questions/20039980/java-replace-line-in-text-file – candied_orange Jan 17 '15 at 05:08
  • 1
    If all the editing you need to do can be added in at the end of the file, then you can append information using java's FileWriter class. FileWriter fw = new FileWriter("path.txt", true); PrintWriter pw = new PrintWriter(fw); pw.println("Append successful."); pw.close(); – Woodrow Jan 17 '15 at 05:59

1 Answers1

0

Better would be deleting the old file and then renaming the temp file instead of overwriting the old file from performance perspective as explained in this example Modifying existing file content in Java

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314