Java Path Copy nio copy(Path source, Path destination)

Here you can find the source of copy(Path source, Path destination)

Description

copy

License

Apache License

Declaration

public static void copy(Path source, Path destination) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.FileNotFoundException;
import java.io.IOException;

import java.nio.file.DirectoryStream;

import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.StandardCopyOption;

public class Main {
    public static void copy(Path source, Path destination) throws IOException {
        if (source.equals(destination)) {
            return;
        } else if (!Files.exists(source)) {
            throw new FileNotFoundException("No such file/dir to copy: " + source);
        } else if (Files.isDirectory(source)) {
            if (!Files.exists(destination)) {
                Files.createDirectories(destination);
            }/*w w  w.  jav a 2s  . c  om*/
            try (DirectoryStream<Path> files = Files.newDirectoryStream(source)) {
                for (Path file : files) {
                    copy(file, destination.resolve(file.getFileName()));
                }
            }
        } else {
            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
        }
        Files.setLastModifiedTime(destination, Files.getLastModifiedTime(source));
    }
}

Related

  1. copy(final File source, final File target)
  2. copy(final Path source, final Path destination, final CopyOption... options)
  3. copy(Path from, Path to)
  4. copy(Path inPath, Path outPath)
  5. copy(Path source, Path destination)
  6. copy(Path source, Path target)
  7. copy(Path source, Path target, CopyOption... options)
  8. copy(Path sourcePath, Path destinationPath)
  9. copy(Path sourcePath, Path targetPath)