[英]How to delete an image in Java after renaming the file and delete the original file?
當我想更新我的(比如說 orderNumber)並嘗試重命名圖像文件時遇到問題。 假設原始文件 ini MainImage15 我想用 Files.Copy 或 Files.Move 重命名為 MainImage16 ,然后我嘗試在成功復制后刪除。 我得到了這樣的錯誤
java.nio.file.FileSystemException: C:\Users\User\apache\webapps\Promotion\030000\MainImage15.jpg: The process cannot access the file because it is being used by another process.
public void renameFileToFileSystem(final String fileName, final String oldFileLocation, final String newFileLocation) {
Path source = Paths.get(oldFileLocation);
Path destination = Paths.get(newFileLocation);
try {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
Files.delete(source);
} catch (final IOException ioException) {
throw new ContentManagementException(fileName, ioException.getMessage());
}
}
我不知道該怎么做。 我應該使用緩沖區關閉嗎? 但我只是重命名文件。 謝謝你。
我不認為Files.copy
保持文件句柄打開。
為了確保它是真實的 - 刪除行Files.copy
並重新運行 - 您仍然可能無法刪除該文件。
所以你必須找到誰讓句柄忙。 基本上有兩種可能:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.