Here you can find the source of copyFile(Path source, Path target, boolean prompt, boolean preserve)
Parameter | Description |
---|---|
source | a parameter |
target | a parameter |
prompt | a parameter |
preserve | a parameter |
public static void copyFile(Path source, Path target, boolean prompt, boolean preserve)
//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")); } }