Carey Brown wrote: Do delete first, then renameTo
Carey Brown wrote: I wouldn't open the file for append. What if the renameTo failed.
Carey Brown wrote:You might want to look into the "Files" utility class
Campbell Ritchie wrote:Why are you using input streams and output streams for text files?
Campbell Ritchie wrote:Is the old file completely closed before you try to rename the new file?
Campbell Ritchie wrote:Where are you renaming your file? You haven't shown us that code, nor told us what happens.
You won't get exceptions with the methods that you are using. You will get a boolean which you are currently not doing anything with. And, even with a boolean that won't tell you what went wrong. Use the methods in "Files".
Miljan Puletic wrote:No exceptions poping up, no runtime errors, nothing...Just "temporary.txt" appears in "data" package, containing edited object/line, which I so desperately want to..
Tim Holloway wrote: 1. Copy the original to the new, temporary file.
2. Rename (NOT delete) the original file to a backup name (filename.bak). If there's an existing backup file, delete IT, then rename.
3. rename the new, temporary file to live under the original filename.
Not necessarily, no. That code won't be reached if an exception is thrown by the preceding code. Nor will the writer be closed if closing the reader throws an exception. That wasn't the correct way to close readers and writers even ten years ago, before Java7; the close() statements had to go in finally blocks. Since Java7, which is ten years old, the correct way to close such objects has been to use try with resources.
Miljan Puletic wrote:. . . Aren't the files closed with
. . . . .
Carey Brown wrote:
Miljan Puletic wrote:
Thank you Tim for replying. I tried to do what you said in this manner, but didn't get back desired result.
Unfortunately you cut off the critical first lines of this error message.
Miljan Puletic wrote:I got this message
Campbell Ritchie wrote:Well are you renaming the files successfully?
Campbell Ritchie wrote:Use try‑with‑resources, as I told you earlier.