Java Path Copy nio copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)

Here you can find the source of copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)

Description

Copy source file to target location.

License

Apache License

Parameter

Parameter Description
source the source directory
target the target directory
okToOverwrite The okToOverwrite parameter determines if an existing target can be overwritten
preserveAttributes The preserveAttributes parameter determines if file attributes should be copied/preserved.

Declaration

public static void copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)
        throws IOException 

Method Source Code


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

import java.io.IOException;

import java.nio.file.CopyOption;

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

import java.nio.file.StandardCopyOption;

public class Main {
    /**/* www.  j  av  a 2 s.c  o  m*/
     * Copy source file to target location.
     * @param source   the source directory
     * @param target   the target directory 
     * @param okToOverwrite The {@code okToOverwrite} parameter determines if an existing target can be overwritten
     * @param preserveAttributes The {@code preserveAttributes} parameter determines if file attributes should be copied/preserved.
     */
    public static void copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)
            throws IOException {
        CopyOption[] options = (preserveAttributes)
                ? new CopyOption[] { StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING }
                : new CopyOption[] { StandardCopyOption.REPLACE_EXISTING };
        if (Files.notExists(target) || okToOverwrite) {
            Files.copy(source, target, options);
        }
    }
}

Related

  1. copyFile(Path source, Path destination)
  2. copyFile(Path source, Path destination, CopyOption... options)
  3. copyFile(Path source, Path target)
  4. copyFile(Path source, Path target)
  5. copyFile(Path source, Path target, boolean foreign, CopyOption... options)
  6. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  7. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  8. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  9. copyFile(Path srcFile, Path srcDir, Path dstDir)