Java Path Copy nio copyFile(Path source, Path destination, CopyOption... options)

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.FileAlreadyExistsException;

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

import java.nio.file.StandardCopyOption;

public class Main {
    public static void copyFile(Path source, Path destination, CopyOption... options) throws IOException {
        if (source == null) {
            throw new IllegalArgumentException("Source cannot be null.");
        }/*w w w  .j  av  a2s.  c  om*/
        if (destination == null) {
            throw new IllegalArgumentException("Destination cannot be null.");
        }
        if (!Files.exists(source)) {
            throw new FileNotFoundException(source.toString());
        }
        if (Files.exists(destination) && !arrayContains(options, StandardCopyOption.REPLACE_EXISTING)) {
            throw new FileAlreadyExistsException(destination.toString());
        }

    }

    private static boolean arrayContains(CopyOption[] options, StandardCopyOption replaceExisting) {
        if (options == null || options.length == 0) {
            return false;
        }
        for (int i = 0; i < options.length; i++) {
            if (options.equals(replaceExisting)) {
                return true;
            }
        }
        return false;
    }
}

Related

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