Little question that NIO 2 of Java 7 has made life simpler for Java programmers and supplies helpful instruments to cope with information and directories, the onus is now on Java builders to study and make the very best use of it.
On this article, I will present you two methods to repeat a listing with information in Java. The primary possibility is through the use of FileUtils class from Apache Commons IO. You should utilize FileUtils.copyDirectory(src, goal) methodology to repeat or backup directories in Java.
Btw, I’m anticipating that you’re accustomed to primary Java Programing and Java API on the whole. If you’re an entire newbie then I recommend you first undergo these Java Programming and growth programs to study extra about core Java fundamentals in addition to such gems from Java API.
The second instance will copy the listing construction utilizing NIO2 of Java 7. Its use will use a recursive algorithm to repeat the listing in Java.
Find out how to copy listing utilizing Apache Commons IO FileUtils class? Instance
This was the only strategy to recursively copy a listing with a full file construction previous to Java 7 world. It’s nonetheless very helpful to many Java builders like me who all the time use the Apache Commons library of their Java tasks. In an effort to use Apache Commons IO, it’s good to embody the next Maven dependency:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <model>2.4</model> </dependency>
and right here is the pattern Java perform which copies a listing with information in only one line. That is the advantage of library strategies, it makes your code extra readable. Little question, why Joshua Bloch has beneficial studying and use library strategies in his traditional Efficient Java.
public static void copyDirectoryUsingApache(String from, String to) throws IOException{ File supply = new File(from); File goal = new File(to); FileUtils.copyDirectory(supply, goal); }
You’ll be able to see how straightforward to repeat a full listing in Java with only one line of code. Sure, you possibly can take away the primary two traces of code that are changing String to the File object, in case you straight move File object. Although when you have filenames as String, it is higher to make use of the strategy on this format.
Java Program to Copy a Non-Empty Listing
In an effort to copy a file or a listing in JDK 1.7, merely calling Information.copy(fromPath, toPath) is sufficient, however it should solely work if the supply listing isn’t empty. In an effort to copy a listing with information and subdirectories, it’s good to write your personal Java program to repeat the entire listing construction in Java as proven beneath.
public static void copyDirectory(File sourceDir, File targetDir) throws IOException { if (sourceDir.isDirectory()) { copyDirectoryRecursively(sourceDir, targetDir); } else { Information.copy(sourceDir.toPath(), targetDir.toPath()); } }
// recursive methodology to repeat listing and sub-diretory in Java non-public static void copyDirectoryRecursively(File supply, File goal) throws IOException { if (!goal.exists()) { goal.mkdir(); } for (String little one : supply.checklist()) { copyDirectory(new File(supply, little one), new File(goal, little one)); } }
The strategy copyDirectory(File src, File dest) first checks if the src File object factors to a listing or not if it does then it calls the recursive methodology to repeat the listing, but when it does not then it simply copy the file utilizing Information.copy() methodology.
There are a few extra copy choices which can be helpful like COPY_ATTRIBUTE, which copies the file attribute like permissions to the goal file whereas copying. The precise file attributes supported are file system and platform-dependent, however last-modified-time is supported throughout the platform and copied to the goal file.
One other factor to notice is whether or not to repeat the mushy hyperlinks or not. It is attainable that your listing could comprise symbolic hyperlinks. The copy methodology copies the goal of the hyperlink, as a substitute of the hyperlink itself. So if you wish to copy the hyperlink itself, and never the goal of the hyperlink, specify both the NOFOLLOW_LINKS or REPLACE_EXISTING possibility
Right here is how our newly copied listing seems to be like once I used this methodology to repeat an present listing on my laptop computer.
That is all about the way to copy the nonempty listing in Java with information and folders. If you’re not operating in Java 7, although it is best to as a result of it has numerous efficiency enchancment, you should use Apache Commons IO to repeat the directories with information and folders in Java.
P. S. – To study extra about JDK 7 new File API and different options, you may also see these free Java programs. This comprises many, straightforward to know examples to rapidly study new options of Java 7 together with the brand new file API and it additionally covers Java SE 8.