Copy a file in Java
This post will discuss how to copy a file in Java.
There are several ways to copy the contents of the source file to the specified target file in Java. Each of the following solutions will create the target file if it doesn’t exist. If the target file exists, then that file will be overwritten with the contents of the source file. Note that only the file contents must be copied to the target file, and file attributes can be ignored.
Note that copying a file is not an atomic operation. It is possible that the target file remains incomplete in case of an I/O error, power loss, process termination, etc.
1. Using NIO
From Java 7 onward, we can use java.nio.file.Files, which provides several static methods that operate on files, directories, or other types of files. To copy a file to a target file, we can use its copy(Path source, Path target, CopyOption… options) method.
This method takes the source and the target file, and an optional parameter to specify how the copy is performed. By default, copying fails if the target file already exists, unless the REPLACE_EXISTING option is specified. If the source and target are the same files, the method completes without copying the file. If either the source file or the destination directory does not exist, java.nio.file.NoSuchFileException is thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; class Main { private static void copyFile(File src, File dest) throws IOException { Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); } public static void main(String[] args) { File from = new File("src.txt"); File to = new File("dest.txt"); try { copyFile(from, to); System.out.println("File copied successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
2. Using Guava Library
Guava’s Files class has several utility methods for working with files. We can use its copy(File from, File to) method, which copies all the bytes from one file to another.
This method doesn’t create the directory holding the destination file if it does not exist. It throws java.io.FileNotFoundException if the source file doesn’t exist and java.io.IOException in case of an I/O error. If to and from refer to the same file, java.lang.IllegalArgumentException is thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import com.google.common.io.Files; import java.io.File; import java.io.IOException; class Main { private static void copyFile(File src, File dest) throws IOException { Files.copy(src, dest); } public static void main(String[] args) { File from = new File("src.txt"); File to = new File("dest.txt"); try { copyFile(from, to); System.out.println("File copied successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
3. Using Apache Commons IO
We can also use the FileUtils class from Apache Commons IO library that has the copyFile(File srcFile, File destFile) method, which copies a file to a new location.
The major advantage of using Apache Commons IO is that the directory holding the destination file is created if it does not exist. If the destination file and the source file are the same, this method throws java.io.IOException, and if the source file doesn’t exist, java.io.FileNotFoundException is thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; class Main { private static void copyFile(File src, File dest) throws IOException { FileUtils.copyFile(src, dest); } public static void main(String[] args) { File from = new File("src.txt"); File to = new File("dest.txt"); try { copyFile(from, to); System.out.println("File copied successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
4. Using Stream
We can even write our own copying logic in plain Java using a FileInputStream and a FileOutputStream. All we need is a buffer to copy all the bytes from one file to another. This is demonstrated below:
Note that if the source and target are the same files, the contents of that file will be deleted. If the source file or the destination directory does not exist, java.io.FileNotFoundException is thrown, and java.io.IOException is thrown in case of an I/O error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.*; class Main { public static void copyFile(File src, File dest) throws IOException { try (InputStream is = new FileInputStream(src); OutputStream os = new FileOutputStream(dest)) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } } } public static void main(String[] args) { File from = new File("src.txt"); File to = new File("dest.txt"); try { copyFile(from, to); System.out.println("File copied successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
5. Using FileChannel
To transfer bytes from the source file to the target file, we can use the transferFrom() method from the FileChannel class, which is a channel for reading, writing, mapping, and manipulating a file.
Note that if the source and target are the same files, the contents of that file will be deleted. If the source file or the destination directory does not exist, java.io.FileNotFoundException is thrown, and java.io.IOException is thrown in case of an I/O error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; class Main { public static void copyFile(File src, File dest) throws IOException { try (FileChannel sourceChannel = new FileInputStream(src).getChannel(); FileChannel destChannel = new FileOutputStream(dest).getChannel()) { destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); } } public static void main(String[] args) { File from = new File("src.txt"); File to = new File("dest.txt"); try { copyFile(from, to); System.out.println("File copied successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
That’s all about copying a file in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)