Files.move and Files.copy is throwing java.nio.file.FileAlreadyExistsException
Asked Answered
I

1

14

I want to delete one file and rename another file with the old file but I am not able to move this file as java is throwing java.nio.file.FileAlreadyExistsException Following is the code snippet I am using

static void swapData(String origFilePath, String tempFilePath) throws IOException{

        Path tempPath = FileSystems.getDefault().getPath(tempFilePath);
        Path origPath = FileSystems.getDefault().getPath(origFilePath);
        try{
            String origFileName = null;
            File origFileRef = new File(origFilePath);
            if(Files.exists(origPath)){
                origFileName = origFileRef.getName();
                Files.delete(origPath);
                if(Files.exists(origPath))
                    throw new IOException("cannot able to delete original file");
            }
            if(origFileName != null)
                Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
        }catch(IOException e){
            throw e;
        }
    }

Here is the exception I am recieving enter image description here on Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);

Also when I see this file in windows explorer, its thumbnail is present but cannot able to open it. I am not able to understand why it is happening and If I am using REPLACE_EXISTING, why it is throwing FileAlreadyExistsException exception.

Also I edited the previous question as it is not clearly stated.

Please help.

Anuj

Indigene answered 9/6, 2016 at 9:15 Comment(4)
Can you tell your to and from directories?Planetarium
Sure this isn't a simple local file system permission issue? BTW: I think you've mixed up the paths? 'destPath' is stored to 'moveFrom'. Maybe just a matter of taste, but the destination should rather be the target ("moveTo"). ;) edit Regarding permissions: have you checked if the file is opened and your user has permissions to access it and write to the target directory?Rolan
Please refer the newly stated questionIndigene
Take a look at this : https://mcmap.net/q/337206/-copy-file-in-java-and-replace-existing-target.Hyacinthia
D
19

Check if you have another thread holding on to the same file resource while running Files.move or Files.copy. I had the same exception and file access symptom and was able to resolve it after serializing the file accesses.

Also, by using the REPLACE_EXISTING option when doing Files.copy or Files.move, you no longer need to code the multiple steps of deleting the original file and then renaming the tmp, although Files.move or Files.copy are not guaranteed atomic. There is a ATOMIC_MOVE option, however I don't like the implementation specific guarantee where IOException could be thrown if a file exists already as described by the javadoc.

ATOMIC_MOVE : The move is performed as an atomic file system operation and all other options are ignored. If the target file exists then it is implementation specific if the existing file is replaced or this method fails by throwing an IOException. If the move cannot be performed as an atomic file system operation then AtomicMoveNotSupportedException is thrown. This can arise, for example, when the target location is on a different FileStore and would require that the file be copied, or target location is associated with a different provider to this object.

Dogcatcher answered 27/4, 2017 at 23:32 Comment(2)
Could you explain what mean by serializing the file accesses?Marasmus
It means synchronizing file access when attempting to modify. If another thread has a opened file handle, current thread delete operation will not complete cleanly and subsequent Files.move will failDogcatcher

© 2022 - 2024 — McMap. All rights reserved.