Copying the file help to change the data without modifying the original data. Same as sometimes required a copy of a file and modifying an application. For example, a photo editing application or software has a copy of the original image as a temp file. In this tutorial, you will learn about writing a Java Copy File Program and many related examples of it.
Copying a file in java comes under File Handling. It’s important for to you learn how to Create, Write, Delete and Move Files programmatically in java.
Ways to Copy a File in Java
There are many ways to Copy a file in java, Here is some methods to do it:
- NIO classes in java – java.nio.channels.FileChannel
- Apache Commons IO FileUtils
- Files class – copy() method
- Operation using InputStream and OutputStream
Let’s start to see examples of Java Copy File
All examples in Java copy file with a new name (Rename). You can use the same name for it in the different file path.
1. Using Channel – NIO classes in java
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class CopyFileExample { public static void main(String[] args) { File oldFile =new File("test.txt"); File newFile =new File("cook.txt"); try { copyFileUsingNIO(oldFile,newFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFileUsingNIO(File sourceFile, File destinationFile) throws IOException { FileInputStream inputStream = new FileInputStream(sourceFile); FileOutputStream outputStream = new FileOutputStream(destinationFile); FileChannel inChannel = inputStream.getChannel(); FileChannel outChannel = outputStream.getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { inChannel.close(); outChannel.close(); inputStream.close(); outputStream.close(); } } }
Output: This output will common for all example of file copying in java
2. Apache Commons IO FileUtils
Apache Commons IO has one method FileUtils.copyFile(File sourceFile, File destination file) easily copy a file. If you are using an Apache Commons IO in the project then you can easily use its methods and classes.
A link to the library – https://commons.apache.org/proper/commons-io/download_io.cgi
*Apache Commons IO 2.6 (requires JDK 1.7+)
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class CopyFileExample { public static void main(String[] args) { File oldFile =new File("test.txt"); File newFile =new File("cook.txt"); try { copyFileUsingApacheCommonsIO(oldFile,newFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { FileUtils.copyFile(source, dest); } }
3. Files class – copy() method
If you are using a using Java SE 7 Files class, then copy() method simply do your work without a lot of code line.
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class CopyFileExample { public static void main(String[] args) { File oldFile =new File("test.txt"); File newFile =new File("cook.txt"); try { copyFileUsingJava7Files(oldFile,newFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); } }
4. Operation using InputStream and OutputStream
This is a basic way to do copy a file. It is slower than other methods because of Blocking IO and fully stream-oriented.
import java.io.*; public class CopyFileExample { public static void main(String[] args) { File oldFile = new File("test.txt"); File newFile = new File("cook.txt"); try { copyFileUsingIO(oldFile, newFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFileUsingIO(File sourceFile, File destinationFile) throws IOException { InputStream inputStreamData = null; OutputStream outputStreamData = null; try { inputStreamData = new BufferedInputStream(new FileInputStream(sourceFile)); outputStreamData = new BufferedOutputStream(new FileOutputStream(destinationFile)); byte[] buffer = new byte[1024]; int length; while ((length = inputStreamData.read(buffer)) > 0) { outputStreamData.write(buffer, 0, length); } } finally { inputStreamData.close(); outputStreamData.close(); } } }
How Java copy directory?
Copying a Java copy folder or directory is the same. See the below example, how to copy a directory from one place to another place. Note- it will only copy the directory on files (inside files in the folder).
Using Files class – copy() method, you can use apache Apache Commons API for copying directory in Java.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CopyFileExample { public static void main(String[] args) { Path sourceDirectory = Paths.get("dest"); Path targetDirectory = Paths.get("dest1"); //copy source to target using Files Class try { Files.copy(sourceDirectory, targetDirectory); } catch (IOException e) { e.printStackTrace(); } } }
Output: You can see the output of example, only folder copy not file.
Question: How to copy multiple files from one directory to another in java?
Answer: You can do it Using one of the above methods. First, count and get the file path with the name of files using for-loop. Then apply any one of a method inside a loop to do until copy all of files.
You must follow this tutorial and example – Java Move File
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Copy File is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.|