我正在编写代码,使用Java NIO API创建一个目录。我的代码片段如下:
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);
但是它抛出了一个错误
java.nio.file.FileAlreadyExistsException: /home/ritesh/Desktop/userA
我知道原因是因为一个同名的目录已经存在,但我想用我的代码生成的目录来覆盖userA目录,该如何实现?