java files.move_Files.move和Files.copy引發java.nio.file.FileAlreadyExistsException

我想刪除一個文件並用舊文件重命名另一個文件,但是我不能移動這個文件,因爲java拋出了java.nio.file.FileAlreadyExistsException以下是代碼段我使用Files.move和Files.copy引發java.nio.file.FileAlreadyExistsException

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;

}

}

這裏是我的Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);

recieving bFvbe.png 異常此外,當我看到在Windows資源管理器這個文件,它的縮略圖是存在的,但能不能打開它。我無法理解它爲什麼會發生,如果我正在使用REPLACE_EXISTING,爲什麼它會拋出FileAlreadyExistsException異常。

另外我編輯了上一個問題,因爲它沒有明確說明。

請幫助。

Anuj

2016-06-09

aga

+0

你能告訴你**到**和**目錄嗎? –

+1

當然,這不是一個簡單的本地文件系統權限問題? 順便說一句:我認爲你混淆了路徑? 'destPath'被存儲到'moveFrom'。也許只是一個品味問題,但目標應該是目標(「moveTo」)。 ;) **編輯**關於權限:您是否檢查過該文件是否已打開,並且您的用戶有權訪問該文件並寫入目標目錄? –

+0

請參考新陳述的問題 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值