Copy and Replace Directory Entries – Java I/O: Part II

Copy and Replace Directory Entries

Consider copying the source file:

./project/src/pkg/Main.java

to the destination file:

Click here to view code image

./project/archive/src/pkg/Main.java

The numbers below refer to corresponding lines in the code below to illustrate copying of files:

(1) Creates the Path object that denotes the source file.

(2) Creates the Path object for the parent path of the destination file. This parent path must exist.

(3) Resolves the parent path with the source pathname, so that if the destination file is to have the same name as the source file, its Path object can be constructed from the parent path and the file name of the source file.

Items (4) through (6) illustrate the scenario when calling the copy() method successively.

(4) Creates the destination file and copies the contents of the source file to the destination file.

(5) Overwrites the contents of the destination file with the contents of the source file.

(6) Fails, as the destination file already exists.

Click here to view code image

Path source = Path.of(“project”, “src”, “pkg”, “Main.java”);               // (1)
Path parentDestinationPath = Path.of(“project”, “archive”, “src”, “pkg”);  // (2)
Path destination = parentDestinationPath.resolve(source.getFileName());    // (3)
Files.copy(source, destination);      // (4) OK. Destination file does not exist.
Files.copy(source, destination,       // (5) OK. Destination file replaced.
           StandardCopyOption.REPLACE_EXISTING);
Files.copy(source, destination);      // (6) FileAlreadyExistsException

The copy() method does not copy the entries in a source directory to a destination directory. The code below attempts to copy the source directory:

./project/src

to the destination directory:

./project/backup

Possible outcomes of the following copying operation can be any of the bulleted options listed below:

Click here to view code image

Path srcDir  = Path.of(“project”, “src”);
Path destDir = Path.of(“project”, “backup”);
Files.copy(srcDir, destDir, StandardCopyOption.REPLACE_EXISTING);

  • If an entry named backup does not exist in the project directory, an empty directory named backup is created.
  • If an entry named backup exists in the project directory and it is an empty directory, a new empty directory named backup is created.
  • If an entry named backup exists in the project directory and it is a file, the file is deleted and an empty directory named backup is created.
  • If an entry named backup exists in the project directory and it is a non-empty directory, the copying operation fails with a DirectoryNotEmptyException.

Another special case to consider is copying a file to a directory. Consider the scenario when copying the source file:

./project/src/pkg/Main.java

to the destination directory:

./project/classes

The code and possible outcomes are outlined below.

Click here to view code image

Path srcFile = Path.of(“project”, “src”, “pkg”, “Main.java”);
Path destDir = Path.of(“project”, “classes”);
Files.copy(srcFile, destDir, StandardCopyOption.REPLACE_EXISTING);

  • If an entry named classes does not exist in the project directory, a file named classes is created and the contents of the source file are copied to the file classes.
  • If an entry named classes exists in the project directory and it is a file, the file is deleted, a new file named classes is created, and the contents of the source file are copied to the file classes.
  • If an entry named classes exists in the project directory and it is an empty directory, the directory is deleted, a file named classes is created, and the contents of the source file are copied to the file classes.
  • If an entry named classes exists in the project directory and it is a non-empty directory, the copying operation fails with a DirectoryNotEmptyException.
Categories: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *