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

CopyUtility source file to target location.

License

Open Source License

Parameter

Parameter Description
source a parameter
target a parameter
prompt a parameter
preserve a parameter

Declaration

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

Method Source Code


//package com.java2s;
import java.nio.file.*;
import static java.nio.file.StandardCopyOption.*;

import java.io.IOException;

public class Main {
    /**/*from w w  w.j av  a 2  s  .  c  o m*/
     * CopyUtility 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.
     * @param source
     * @param target
     * @param prompt
     * @param preserve
     */
    public 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);
            }
        }
    }

    /**
     * Returns {@code true} if okay to overwrite a  file ("cp -i")
     * @param file
     * @return 
     */
    public static boolean okayToOverwrite(Path file) {
        String answer = System.console().readLine("overwrite %s (yes/no)? ", file);
        return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
    }
}

Related

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