Files copy() Method in Java with Examples – GeeksforGeeks

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.

  1. Using copy(InputStream in, Path target, CopyOption… options) Method
  2. Using copy(Path source, OutputStream out) Method
  3. 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

Tóm Tắt

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 GFG {

 

    

    public static void main(String[] args)

    {

        

        String text = "geeksforgeeks";

 

        

        Path path = (Path)Paths.get("/usr", "local", "bin",

                                    "fileIn.txt");

 

        System.out.println("Path of target file: "

                           + path.toString());

 

        

        InputStream in

            = new ByteArrayInputStream(text.getBytes());

 

        

        try {

 

            

            System.out.println(

                "Number of bytes copied: "

                + Files.copy(

                    in, path,

                    StandardCopyOption.REPLACE_EXISTING));

        }

 

        

        catch (IOException e) {

 

            

            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




import 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;

 

public class GFG {

 

    

    public static void main(String[] args)

    {

 

        

        Path path = (Path)Paths.get("/usr", "local", "bin",

                                    "fileOut.txt");

 

        

        System.out.println("Path of source file: "

                           + path.toString());

 

        

        OutputStream out = new ByteArrayOutputStream();

 

        

        try {

            

            System.out.println("Number of bytes copied: "

                               + Files.copy(path, out));

        }

 

        

        catch (IOException e) {

 

            

            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




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 GFG {

 

    

    public static void main(String[] args)

    {

        

        Path pathIn = (Path)Paths.get("/usr", "local",

                                      "bin", "fileIn.txt");

        

        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 {

 

            

            System.out.println(

                "Number of bytes copied: "

                + Files.copy(

                    pathOut, pathIn,

                    StandardCopyOption.REPLACE_EXISTING));

        }

 

        

        catch (IOException e) {

 

            

            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 

My Personal Notes

arrow_drop_up