【问题标题】:How to move folder from one location to another location using java如何使用java将文件夹从一个位置移动到另一个位置
【发布时间】:2015-03-04 18:23:40
【问题描述】:

如何将一个文件夹从一个位置移动到另一个位置? 这是我所做的示例代码,但这里显示的是java.nio.file.NoSuchFileException 我正在使用这个包: import java.nio.file.Files; import java.nio.file.StandardCopyOption;

Path path1 = FileSystems.getDefault().getPath("D:\\VFSImagecomp\\compressed\\");
Path path2 = FileSystems.getDefault().getPath("D:\\destinitionFile\\");

Files.move(path1, path2, StandardCopyOption.REPLACE_EXISTING);

我正在尝试将压缩文件夹移动到destinitionFile Folder。但它不起作用。你能建议我吗?

【问题讨论】:

  • 我会尝试重命名它。
  • @manwearraj - 我会使用System.getProperty("path.separator") 自动确定所需的``,因此您无需猜测是其中的一两个。顺便说一句:-)

标签: java file filesystems filepath


【解决方案1】:

您需要指定目的地的名称,否则它将替换您的parent 文件夹(因为您使用的是REPLACE_EXISTING

 Path path1 = FileSystems.getDefault().getPath("D:\\VFSImagecomp\\compressed");
 Path path2 = FileSystems.getDefault().getPath("D:\\destinitionFile\\myNewDirectory");

如果你想保持相同的名字,那么:

 Path path2 = FileSystems.getDefault().getPath("D:\\destinitionFile\\compressed");

【讨论】:

  • 感谢您的回复...现在它正在工作。假设如果运行第二个此应用程序,它会显示 DirectoryNotEmptyException...但是我已经放置了 REPLACE_EXISTING,为什么它不工作...@gttgaxiola跨度>
  • Source 状态:REPLACE_EXISTING – Performs the copy even when the target file already exists. If the target is a symbolic link, the link itself is copied (and not the target of the link). If the target is a non-empty directory, the copy fails with the FileAlreadyExistsException exception.
  • 要么重命名目标目录,要么必须在移动之前删除现有文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
相关资源
最近更新 更多