It's easy to copy a file or empty directory in Java as you can use Files.copy(fromPath, toPath) from Java 7, but, unfortunately, it's not as easy to copy a non-empty directory with all its files and subdirectories in Java, much like deleting a non-empty directory. There is no method in Java IO API which copies everything inside one directory to another. The copy(source, target, CopyOption...) method can copy directories, but files inside the directories are not copied. So the new directory will be empty even if the original directory contains files and folders. Similarly, the copy fails if target directory already exists, unless the REPLACE_EXISTING copy option is specified. No doubt that NIO 2 of Java 7 has made life easier for Java programmers and provides useful tools to deal with files and directories, the onus is now on Java developers to learn and make the best use of it. If you to learn more about NIO 2 features, see here.
Read more »
No comments:
Post a Comment