There is several ways to copy file from one location to another. 4 ways will be shown here :
- Writing a InputStream to an OutputStream
- Using FileChannel
- Using java.nio.file.Files
- Using Apache Commons IO FileUtils
Table of contents
1- Writing a InputStream to an OutputStream
JDK 1.0+
First step to copy files is by using basic Java code by reading a InputStream (source file) and writing it to a OuputStream that corresponds to your destination file. Below an example :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Basic file copier. * * @author Radouane ROUFID. * */ public class BasicFileCopier { public static void main(String[] args) { // Source file. File source = new File("C:/dev/file.txt"); // Destination file. File destination = new File("C:/dev/files/file.txt"); try { copyFile(source, destination); } catch (IOException e) { System.out.println("Problem occurs while copying files"); e.printStackTrace(); } } /** * Copy file. * * @param source * File to copy. * @param destination * Copy destination. * @throws IOException */ public static void copyFile(File source, File destination) throws IOException { // Using try with resources (No need to close files). try (InputStream inputStream = new FileInputStream(source); OutputStream outputStream = new FileOutputStream(destination);) { // Max length per line = 1024. byte[] buffer = new byte[1024]; int lineLength; while ((lineLength = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, lineLength); } } } } |
Note that i used Try with resources. Files are closed automatically.
2- Using fileChannel
JDK 1.4+
You can use the transferFrom or transfertTo methods of FileChannel to make the copy. Below how to do :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * FileChannel copier. * * @author Radouane ROUFID. * */ public class FileChannelCopier { public static void main(String[] args) { // Source file. File source = new File("C:/dev/file.txt"); // Destination file. File destination = new File("C:/dev/files/file.txt"); try { copyFile(source, destination); } catch (IOException e) { System.out.println("Problem occurs when copying files"); e.printStackTrace(); } } /** * Copy file. * * @param source * File to copy. * @param destination * Copy destination. * @throws IOException */ public static void copyFile(File source, File destination) throws IOException { try (FileInputStream inputStream = new FileInputStream(source); FileOutputStream outputStream = new FileOutputStream(destination);){ FileChannel sourceChannel = inputStream.getChannel(); FileChannel destinationChannel = outputStream.getChannel(); destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); } } } |
3- Using java.nio.file.Files
JDK 1.7+
Since Java 1.7, the static class java.nio.file.Files is given to operate on files and directories. It provides the method Copy to copy files. Below of to use it :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; /** * Files copier. * * @author Radouane ROUFID. * */ public class FilesCopier { public static void main(String[] args) { // Source file. Path source = Paths.get("C:/dev/file.txt"); // Destination file. Path destination = Paths.get("C:/dev/files/file.txt"); try { copyFile(source, destination); } catch (IOException e) { System.out.println("Problem occurs when copying files"); e.printStackTrace(); } } /** * Copy file. * * @param source * File to copy. * @param destination * Copy destination. * @throws IOException */ public static void copyFile(Path source, Path destination) throws IOException { Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } } |
4- Using Apache Commons IO FileUtils
Since JDK 1.6 For Commons IO 2.4
Since JDK 1.5 For Commons IO 2.2
Appache Commons IO provides FileUtils to handle files and folders. It can be useful to copy files.
-
Download the dependency
If you are working on a Maven project, add the following to your pom.xml
1 2 3 4 5 6 7 |
<dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> |
If not, download the jar and add it to your classpath.
-
Copy files
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 32 33 34 |
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * @author RR33ABCN * */ public class FileUtilsCopier { /** * @param args */ public static void main(String[] args) { // Source file. File source = new File("C:/dev/file.txt"); // Destination file. File destination = new File("C:/dev/files/file.txt"); try { copyFile(source, destination); } catch (IOException e) { System.out.println("Problem occurs when copying files"); e.printStackTrace(); } } public static void copyFile(File source, File destination) throws IOException { FileUtils.copyFile(source, destination); } } |