copyFileToDirectory()
is a FileUtils
class that is used to copy a file to a directory, with the option to preserve the filing date.
This method copies the contents of the specified source file to a file of the same name in the specified destination directory.
The destination directory is created if it does not exist. If the destination file does exist, then this method will overwrite it.
FileUtils
The definition of FileUtils
can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
For other versions of the
commons-io
package, refer to the Maven Repository.
You can import the FileUtils
class as follows:
import org.apache.commons.io.FileUtils;
public static void copyFileToDirectory(final File sourceFile, final File destinationDir, final boolean preserveFileDate)
final File sourceFile
: This is the file to copy.final File destinationDir
: This is the destination directory.final boolean preserveFileDate
: This is a Boolean flag that dictates whether or not to preserve the file dates.This method does not return anything.
import org.apache.commons.io.FileUtils;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.Arrays;public class Main{private static void wrapper(String srcFilePath, String dstDir) throws IOException {File srcFile = new File(srcFilePath);File dstDirFile = new File(dstDir);System.out.println("The contents of the destination directory before copy - " + Arrays.toString(dstDirFile.listFiles()));System.out.println("Copying file - " + srcFilePath + " to directory - " + dstDir);FileUtils.copyFileToDirectory(srcFile, dstDirFile, false);System.out.println("The contents of the destination directory after copy - " + Arrays.toString(dstDirFile.listFiles()));}public static void main(String[] args) throws IOException {// Example 1String srcfilePath = "/Users/educative/Documents/temp/1.txt";String dstDir = "/Users/educative/Documents/temp-1";wrapper(srcfilePath, dstDir);System.out.println("-------");// Example 2srcfilePath = "/Users/educative/Documents/temp/5.txt";dstDir = "/Users/educative/Documents/temp-1";wrapper(srcfilePath, dstDir);}}
source file - /Users/educative/Documents/temp/1.txt
destination directory - /Users/educative/Documents/temp-1
The source file is copied to the destination directory. The contents of the destination directory before and after the copy are as follows:
Before Copy - []
After Copy - [/Users/educative/Documents/temp-1/1.txt]
source file - /Users/educative/Documents/temp/5.txt
destination directory - /Users/educative/Documents/temp-1
The method throws FileNotFoundException
, as the source file is not available for the copy operation.
In the above code, we have a wrapper method that takes in the source file and the destination directory.
The wrapper method copies the source file to the destination directory with the help of FileUtils.copyFileToDirectory()
.
The wrapper method also prints the content of the destination directory before and after the copy operation.
The output of the code is as follows:
The contents of the destination directory before copy - [/Users/educative/Documents/temp-1/1.txt]
Copying file - /Users/educative/Documents/temp/1.txt to directory - /Users/educative/Documents/temp-1
The contents of the destination directory after copy - [/Users/educative/Documents/temp-1/1.txt]
-------
The contents of the destination directory before copy - [/Users/educative/Documents/temp-1/1.txt]
Copying file - /Users/educative/Documents/temp/5.txt to directory - /Users/educative/Documents/temp-1
Exception in thread "main" java.io.FileNotFoundException: File system element for parameter 'source' does not exist: '/Users/educative/Documents/temp/5.txt'
at org.apache.commons.io.FileUtils.requireExistsChecked(FileUtils.java:2719)
at org.apache.commons.io.FileUtils.requireFileCopy(FileUtils.java:2751)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:840)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:783)
at org.apache.commons.io.FileUtils.copyFileToDirectory(FileUtils.java:926)
at Main.wrapper(Main.java:26)
at Main.main(Main.java:40)