Files copy() Method in Java with Examples

The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files.
Methods: Based on the type of arguments passed, the Files class provides 3 types of copy() method.
- Using copy(InputStream in, Path target, CopyOption… options) Method
- Using copy(Path source, OutputStream out) Method
- Using copy(Path source, Path target, CopyOption… options) Method
Method 1: Using copy(InputStream in, Path target, CopyOption… options) Method
This method is used to copy all bytes from an input stream to a file.
Parameters:
- in: The input stream whose the data will be copied
- target: The path of the file where data will be copied
- options: Options describing the ways in which the data will be copied
Return Type: Number of copied bytes
Exceptions:
- IOException: If while reading or writing an error occurred
- FileAlreadyExistsException: If the target file already exists and can not be replaced
- DirectoryNotEmptyException: If the target file can not be replaced because it is a non-empty directory
- UnsupportedOperationException: If the way of copying described by an option is not supported
- SecurityException: If the write access to the target file is denied by the security manager
Implementation:
Example
Java
// Importing classes from java.nio package as// this package is responsible for network linkingimport 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;// Main Classpublic class GFG { // Main driver method public static void main(String[] args) { // Input custom string String text = "zambiatek"; // Path of the file where data is to be copied Path path = (Path)Paths.get("/usr", "local", "bin", "fileIn.txt"); System.out.println("Path of target file: " + path.toString()); // Byte stream whose data is to be copied InputStream in = new ByteArrayInputStream(text.getBytes()); // Try block to check for exceptions try { // Printing number of copied bytes System.out.println( "Number of bytes copied: " + Files.copy( in, path, StandardCopyOption.REPLACE_EXISTING)); } // Catch block to handle the exceptions catch (IOException e) { // Print the line number where exception occurred e.printStackTrace(); } }} |
Output:
Path of target file: /usr/local/bin/fileIn.txt Number of bytes copied: 13
Method 2: Using copy(Path source, OutputStream out) Method
This method is used to copy all bytes from a file to an output stream.
Parameters: It takes two namely
- source: The path of the file whose data will be copied
- out: The output stream where the copied data will be written
Return Type: Number of copied bytes
Exceptions:
- IOException: If while reading or writing an error occurred
- SecurityException: If the read access to the target file is denied by the security manager
Implementation:
Example
Java
// Importing classes from java.nio package as// this package is responsible for network linkingimport java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;// Main Classpublic class GFG { // Main driver method public static void main(String[] args) { // Path of file whose data is to be copied Path path = (Path)Paths.get("/usr", "local", "bin", "fileOut.txt"); // Getting the path of source file System.out.println("Path of source file: " + path.toString()); // Output stream where data is to written OutputStream out = new ByteArrayOutputStream(); // Try block to check for exceptions try { // Printing number of copied bytes System.out.println("Number of bytes copied: " + Files.copy(path, out)); } // Catch block to handle the exception catch (IOException e) { // Print the line number where exception occurred e.printStackTrace(); } }} |
Output:
Path of source file: /usr/local/bin/fileOut.txt Number of bytes copied: 13
Method 3: Using copy(Path source, Path target, CopyOption… options) Method
This method is used to copy a file to a target file.
Parameters:
- source: The path of the file whose data will be copied
- target: The path of the file where data will be copied
- options: Options describing the ways in which the data will be copied
Return Type: The path to the file where data is copied
Exceptions:
- IOException: If while reading or writing an error occurred
- FileAlreadyExistsException: If the target file already exists and can not be replaced
- DirectoryNotEmptyException: If the target file can not be replaced because it is a non-empty directory
- UnsupportedOperationException: If the way of copying described by an option is not supported
- SecurityException: If the write access to the target file is denied by the security manager
Implementation:
Example
Java
// Importing classes from java.nio package as// this package is responsible for network linkingimport java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.StandardCopyOption;// Main Classpublic class GFG { // Main driver method public static void main(String[] args) { // Path of file where data is to copied Path pathIn = (Path)Paths.get("/usr", "local", "bin", "fileIn.txt"); // Path of file whose data is to be copied Path pathOut = (Path)Paths.get( "/usr", "local", "bin", "fileOut.txt"); System.out.println("Path of target file: " + pathIn.toString()); System.out.println("Path of source file: " + pathOut.toString()); // Try block to check for exceptions try { // Printing number of bytes copied System.out.println( "Number of bytes copied: " + Files.copy( pathOut, pathIn, StandardCopyOption.REPLACE_EXISTING)); } // Catch block to handle the exceptions catch (IOException e) { // Print the line number where exception occurred e.printStackTrace(); } }} |
Output:
Path of target file: /usr/local/bin/fileIn.txt Path of source file: /usr/local/bin/fileOut.txt Number of bytes copied: 13



