Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
fileutils

What is FileUtils.copyFileToDirectory() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

copyFileToDirectory() is a staticThis describes the methods in Java that can be called without creating an object of the class. method of the 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.

How to import 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;

Syntax


public static void copyFileToDirectory(final File sourceFile, final File destinationDir, final boolean preserveFileDate)

Parameters

  • 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.

Return value

This method does not return anything.

Code

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 1
String srcfilePath = "/Users/educative/Documents/temp/1.txt";
String dstDir = "/Users/educative/Documents/temp-1";
wrapper(srcfilePath, dstDir);
System.out.println("-------");
// Example 2
srcfilePath = "/Users/educative/Documents/temp/5.txt";
dstDir = "/Users/educative/Documents/temp-1";
wrapper(srcfilePath, dstDir);
}
}

Example 1

  • 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]

Example 2

  • 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.

Explanation

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.

Output

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)


RELATED TAGS

java
communitycreator
fileutils

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring