nio - can any process read the target file when my java process renaming the source file -
our portal website read json file display information. json file generated jobs wrote java.
i concerned if java progress write json directly, website process may incomplete file because java process writing file.
so decide write information temp file, after temp file ok, rename target file, website process complete json file.
but still concerned when renaming file, can process read intermediate status of target file. actually, don't know how java implements rename action.
my code below:
path source = filesystems.getdefault().getpath("/data/temp/temp.7z.bak"); files.move(source, source.resolvesibling("/data/temp/temp.7z"), standardcopyoption.replace_existing);
you may want use atomic_move
option in method call :
files.move(source, source.resolvesibling("/data/temp/temp.7z"), standardcopyoption.atomic_move));
indeed , documentation states that
atomic_move
the move performed atomic file system operation , other options ignored. if target file exists implementation specific if existing file replaced or method fails throwing ioexception. if move cannot be performed atomic file system operation atomicmovenotsupportedexception thrown. can arise, example, when target location on different filestore , require file copied, or target location associated different provider object.
this should guarantee file gets moved entirely without intermediate states.
note atomicmovenotsupportedexception
if such atomic operation not possible.
Comments
Post a Comment