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 = "neveropen";Â
        // 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
