我在我的程序中使用了 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
繁星淼淼
www说
相关分类