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

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

Description

Safely copies file, creates destination folder if needed.

License

Open Source License

Parameter

Parameter Description
source path to the file
destination path to file's destination.

Exception

Parameter Description
IOException thrown on filesystem error.

Declaration

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

Method Source Code


//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.IOException;

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

public class Main {
    /**/* www. ja  va 2s. c  o m*/
     * Safely copies file, creates destination folder if needed.
     *
     * @param source
     *        path to the file
     * @param destination
     *        path to file's destination.
     * @throws IOException
     *         thrown on filesystem error.
     */
    public static void copyFile(Path source, Path destination) throws IOException {
        final Path destFolders = destination.getParent();
        if (Files.notExists(destFolders)) {
            Files.createDirectories(destFolders);
        }
        Files.copy(source, destination);
    }

    /**
     * Safely copies files and directories with its content.
     *
     * @param source
     *        path to the existing file.
     * @param destination
     *        path to the desired location.
     * @throws IllegalArgumentException
     *         thrown on failure to resolve filename conflict.
     * @throws IOException
     *         thrown on filesystem error.
     */
    public static void copy(Path source, Path destination) throws IllegalArgumentException, IOException {
        if (Files.isDirectory(source)) {
            if (Files.exists(destination)) {
                if (!Files.isDirectory(destination)) {
                    throw new IllegalArgumentException(
                            String.format("fail to copy %s to %s " + "because former is not a directory",
                                    source.toString(), destination.toString()));
                }
            } else {
                Files.createDirectory(destination);
            }
            final DirectoryStream<Path> subPaths = Files.newDirectoryStream(source);
            for (Path path : subPaths) {
                final Path relativePath = source.relativize(path);
                copy(path, destination.resolve(relativePath));
            }
            subPaths.close();

        } else {
            Files.copy(source, destination);
        }
    }
}

Related

  1. copyContents(Path from, Path to)
  2. copyEverythingExcept(Path file, Path srcDir, Path dstDir, Predicate excluded, Consumer onCopy)
  3. copyFile(final String fileName, final String from, final String to)
  4. copyFile(InputStream from, Path to)
  5. copyFile(Path from, Path to)
  6. copyFile(Path source, Path destination, CopyOption... options)
  7. copyFile(Path source, Path target)
  8. copyFile(Path source, Path target)
  9. copyFile(Path source, Path target, boolean foreign, CopyOption... options)