Reputation: 10586
Currently I am browsing a file from my local machine and then copying it to another directory from where I am reading the file. But the copy process is not finished( considering the large size of file) and the reading has already started , so it fails giving me error and everytime I get the error from reading the file, it is on different line.
How can I start reading the file only after copying of the file has finished?
Is there a way to verify that copied file is the same size as of original file?
Upvotes: 0
Views: 80
Reputation: 310860
Don't copy it. Rename it, when you've finished downloading it and have closed it. That's atomic. The moment it appears in the target directory, it is complete. Problem cannot occur. End of story.
It is also O(1) instead of O(N). Gazzilions of times as fast.
Upvotes: 1
Reputation: 7459
The way I've done this in the past is that the process writing the copied file writes to a "temp" file, and then moves the file to the read location when it has finished writing the file.
So the writing process would write to info.txt.tmp. When it's finished, it renames the file to info.txt. The reading process then just had to check for the existence of info.txt - and it knows that if it exists, it has been written completely.
Alternatively you could have the write process write info.txt to a different directory, and then move it to the read directory if you don't like using weird file extensions.
Upvotes: 0