Java Copy File using Files.copy()
Tags: Files.copy Java NIO
In this Java NIO tutorial we learn how to use the Files.copy() method to copy files in Java program. With different detail example Java programs we show you how to copy file to destination location, or copy an InputStream to a file, copy a file to an OutputStream.
Table of contents
How to use Files.copy() method to copy file in Java
For example we have a file at D:\Folder1\image.png and want to copy it to another directory at D:\Folder2\image.png , we can use Files.copy() method as following Java program.
FilesCopyExample1.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesCopyExample1 {
public static void main(String... args) throws IOException {
Path sourceFile = Paths.get("D:\\Folder1\\image.png");
Path destinationFile = Paths.get("D:\\Folder2\\image.png");
Path copiedFile = Files.copy(sourceFile, destinationFile);
System.out.println("Successful copy file to " + copiedFile.toString());
}
}
Successful copy file to D:\Folder2\image.png
In case the destination location already existing the file your application may get the java.nio.file.FileAlreadyExistsException exception as below.
Exception in thread "main" java.nio.file.FileAlreadyExistsException: D:\Folder2\image.png
at sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:124)
at sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:278)
at java.nio.file.Files.copy(Files.java:1274)
at FilesCopyExample1.main(FilesCopyExample1.java:11)
To allow the Java application replace the existing file if it exists we can call the Files.copy() method with StandardCopyOption.REPLACE_EXISTING parameter as example Java program below.
FilesCopyExample2.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FilesCopyExample2 {
public static void main(String... args) throws IOException {
Path sourceFile = Paths.get("D:\\Folder1\\image.png");
Path destinationFile = Paths.get("D:\\Folder2\\image.png");
Path copiedFile = Files.copy(sourceFile, destinationFile, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Successful copy file to " + copiedFile.toString());
}
}
Successful copy file to D:\Folder2\image.png
More Files.copy() Java Examples
Copy FileInputStream to File using Files.copy()
Using the using Files.copy() method we also can copy all bytes in an InputStream into a file. In the following Java program we will copy a FileInputStream to a destination file location.
FilesCopyExample3.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FilesCopyExample3 {
public static void main(String... args) throws IOException {
InputStream inputStream = new FileInputStream("D:\\Folder1\\image.png");
Path destinationFile = Paths.get("D:\\Folder2\\image.png");
long numberOfBytes = Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Successful copy " + numberOfBytes + " bytes " + destinationFile.toString());
}
}
Successful copy 390 bytes D:\Folder2\image.png
Copy ByteArrayInputStream to File using Files.copy()
Or we can copy all bytes from a ByteArrayInputStream to file as below Java program.
FilesCopyExample4.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FilesCopyExample4 {
public static void main(String... args) throws IOException {
String valueToCopy = "Simple Solution";
InputStream inputStream = new ByteArrayInputStream(valueToCopy.getBytes());
Path destinationFile = Paths.get("D:\\Folder2\\text-file.txt");
long numberOfBytes = Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Successful copy " + numberOfBytes + " bytes " + destinationFile.toString());
}
}
Successful copy 15 bytes D:\Folder2\text-file.txt
Copy File to FileOutputStream using Files.copy()
The Files.copy() method allow to copy file to an OutputStream.
In the following Java program, we show you how to copy all bytes of a file to a destination FileOutputStream.
FilesCopyExample5.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesCopyExample5 {
public static void main(String... args) throws IOException {
Path sourceFile = Paths.get("D:\\Folder1\\image.png");
OutputStream outputStream = new FileOutputStream("D:\\Folder2\\image.png");
long numberOfBytes = Files.copy(sourceFile, outputStream);
System.out.println("Successful copy " + numberOfBytes + " bytes to the OutputStream");
}
}
Successful copy 390 bytes to the OutputStream
Copy File to ByteArrayOutputStream using Files.copy()
The following Java program, we show you how to copy all bytes from a file to a ByteArrayOutputStream.
FilesCopyExample7.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesCopyExample7 {
public static void main(String... args) throws IOException {
Path sourceFile = Paths.get("D:\\Folder1\\Document.txt");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
long numberOfBytes = Files.copy(sourceFile, outputStream);
System.out.println("Successful copy " + numberOfBytes + " bytes ");
System.out.println("Copied content:");
System.out.println(new String(outputStream.toByteArray()));
}
}
Successful copy 15 bytes
Copied content:
Simple Solution
Happy Coding 😊
Related Articles
Java Get All Entries in Directory using Files.newDirectoryStream()
Java Create Parent and Sub Directories using Files.createDirectories()