Java Path Copy nio copyFile(Path source, Path target, boolean prompt, boolean preserve)

Here you can find the source of copyFile(Path source, Path target, boolean prompt, boolean preserve)

Description

Copy source file to target location.

License

MIT License

Declaration

private static void copyFile(Path source, Path target, boolean prompt, boolean preserve) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  .j  a  v  a 2  s .c o m
 * Copyright (C) 2011-2014 by Ahmed Osama el-Sawalhy
 *
 *      The Modified MIT Licence (GPL v3 compatible)
 *          License terms are in a separate file (LICENCE.md)
 *
 *      Project/File: Overcast/com.yagasoft.overcast.base.container/FolderHelper.java
 *
 *         Modified: Apr 15, 2014 (8:06:38 AM)
 *            Using: Eclipse J-EE / JDK 7 / Windows 8.1 x64
 */

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.IOException;
import java.nio.file.CopyOption;

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

public class Main {
    /**
     * Copy source file to target location. If {@code prompt} is true then prompt user to overwrite target if it exists. The
     * {@code preserve} parameter determines if file attributes should be copied/preserved.
     */
    private static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
        CopyOption[] options = (preserve) ? new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING }
                : new CopyOption[] { REPLACE_EXISTING };
        if (!prompt || Files.notExists(target)) // || okayToOverwrite(target))
        {
            try {
                Files.copy(source, target, options);
            } catch (IOException x) {
                System.err.format("Unable to copy: %s: %s%n", source, x);
            }
        }
    }
}

Related

  1. copyFile(Path source, Path target)
  2. copyFile(Path source, Path target)
  3. copyFile(Path source, Path target, boolean foreign, CopyOption... options)
  4. copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)
  5. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  6. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  7. copyFile(Path srcFile, Path srcDir, Path dstDir)
  8. copyFile(String srcPath, String destPath)
  9. copyFile(String urlPath, String outFile)