简体   繁体   English

java.nio.file.FileAlreadyExistsException如何在java7中解决这个问题

[英]java.nio.file.FileAlreadyExistsException how to resolve this in java7

i am writing a code i am creating a directory with java nio api my segment of code is 我正在编写一个代码我用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);

but it is throwing an error 但这是一个错误

java.nio.file.FileAlreadyExistsException: /home/ritesh/Desktop/userA

reason i know because a directory already with same name but i want to overwrite userA directory with directory generated by my code directory how to accomplsih this?? 我知道的原因是因为一个目录已经具有相同的名称,但我想用我的代码目录生成的目录覆盖userA目录如何完成这个?

Instead of 代替

Files.createDirectory(target, attr);

try using 尝试使用

Files.createDirectories(target, attr);

In the documentation for Files.createDirectories() : Files.createDirectories()的文档中:

Creates a directory by creating all nonexistent parent directories first. 首先通过创建所有不存在的父目录来创建目录。 Unlike the createDirectory method, an exception is not thrown if the directory could not be created because it already exists. 与createDirectory方法不同,如果由于目录已存在而无法创建目录,则不会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中移动目录会抛出java.nio.file.FileAlreadyExistsException - Moving a directory in java throws java.nio.file.FileAlreadyExistsException Files.move 和 Files.copy 正在抛出 java.nio.file.FileAlreadyExistsException - Files.move and Files.copy is throwing java.nio.file.FileAlreadyExistsException 如何解决java7 nio socket WritePendingException? - how to solve java7 nio socket WritePendingException? Java7异步NIO2服务器上的连接拒绝 - Connection refusal on Java7 async NIO2 server Java7 nio2的任何可用的内存中FileSystem实现? - Any available in-memory FileSystem implementations for Java7 nio2? 使用Java7 NIO在附加模式下打开zipfile内的2.2GB大文件时出现Java错误 - Java Error while opening a large file of 2.2GB in appending mode which is inside a zipfile using java7 NIO 如何在Java7中为特定用户设置文件访问属性 - How to set File Access attribute for a particular user in java7 无法解析类java.nio.file.FileSystems - Unable to resolve class java.nio.file.FileSystems 如何解决 java.nio.file.FileSystemException 进程无法访问该文件,因为它正在被另一个进程使用 - How to resolve java.nio.file.FileSystemException The process cannot access the file because it is being used by another process Path.resolve(其他)java.nio.file.Path方法 - Path.resolve(other) java.nio.file.Path method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM