[英]How to move all files from source directory to destination directory, keeping existing files in destination directory, using Java NIO?
[英]Using Java nio Files,Rename Directory but if already exists move the files to existing Directory
我想將Dir“Dir1”重命名為“Dir2”。
但如果“Dir2”退出,那么我想將“Dir1”文件移動到“Dir2”。 C:\\ Artwork \\ Dir1 C:\\ Artwork \\ Dir2 For Rename
Files.move(path, path.resolveSibling("NewName"))); //Worked Ok
對於Dir已經在場,我試過了
Path source = Paths.get("C:\\Artwork\\Dir1");
Path newDir = Paths.get("C:\\Artwork\\Dir2");
Files.move(source, newDir,StandardCopyOption.ATOMIC_MOVE); //Throwed AccessDeniedException
Files.move(source, newDir,StandardCopyOption.REPLACE_EXISTING); //Throwed DirectoryNotEmptyException
我已經編寫了一些實用程序方法來使用NIO File API復制目錄(該庫是開源的):
Maven的:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.3</version>
</dependency>
教程:
http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html
API: CopyFileVisitor.copy
如果Dir2不存在則使用Dir2.mkdir();
創建目錄。 如果它已經是exixt那么moveDirectory(File Dir1, File Dir2);
我們可以移動整個目錄
從Java 1.7開始,java.nio.file.Files提供了處理文件和目錄的操作。 特別是move,copy和walkFileTree函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.