我在我的程序中使用了 Files.move 方法,如下所述。 public boolean moveAndRenameFile(String targetPath, String newName) { boolean fileMoved = true; try { Path pathToFile = FileSystems.getDefault().getPath(targetPath); Files.move(Paths.get(path), pathToFile.resolve(newName), StandardCopyOption.REPLACE_EXISTING); } catch (InvalidPathException | IOException e) { LOGGER.error("File couldn't be moved from {} to target location {}", path, targetPath); LOGGER.error(e.getMessage(), e); fileMoved = false; } return fileMoved; }如果中間發生任何異常/錯誤,是否有可能從原始位置刪除文件但不移動到目標位置?我瀏覽了以下鏈接,但找不到這個問題的答案。 https://docs.oracle.com/javase/tutorial/essential/io/move.html
添加回答
舉報
0/150
提交
取消