Java.nio.file.FileAlreadyExistsException how to resolve this in java7
I write code i create directory with java nio api my code segment
Path target = Paths.get(""+folder_path+xx[0]);
Set<PosixFilePermission> perms = null;
if(xx[2].toLowerCase().equals("read"))
perms =PosixFilePermissions.fromString("r--------");
if(xx[2].toLowerCase().equals("read/write"))
{
perms =PosixFilePermissions.fromString("rw-------");
}
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createDirectory(target, attr);
but it throws an error
java.nio.file.FileAlreadyExistsException: /home/ritesh/Desktop/userA
I know because the directory is already with the same name, but I want to overwrite the userA directory with the directory generated in my code directory, how to do this.
+3
source to share
1 answer
Instead
Files.createDirectory(target, attr);
try using
Files.createDirectories(target, attr);
The documentation for Files.createDirectories()
:
Creates a directory, creating all nonexistent parent directories first. Unlike the createDirectory method, no exception is thrown if the directory cannot be created because it already exists.
+8
source to share