Java Utililty Methods Path Copy nio

List of utility methods to do Path Copy nio

Description

The list of methods to do Path Copy nio are organized into topic(s).

Method

voidcopyFile(final String fileName, final String from, final String to)
copy File
final Path toDir = Paths.get(to);
if (Files.notExists(toDir)) {
    Files.createDirectories(toDir);
Files.copy(Paths.get(from + fileName), Paths.get(to + fileName), StandardCopyOption.REPLACE_EXISTING);
voidcopyFile(InputStream from, Path to)
copy File
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
voidcopyFile(Path from, Path to)
Copy file
try {
    to.getParent().toFile().mkdirs();
    Files.copy(from, to);
} catch (FileAlreadyExistsException e) {
} catch (IOException e) {
    fatal("An error occured while writing " + to.toString(), e);
    return;
voidcopyFile(Path source, Path destination)
Safely copies file, creates destination folder if needed.
final Path destFolders = destination.getParent();
if (Files.notExists(destFolders)) {
    Files.createDirectories(destFolders);
Files.copy(source, destination);
voidcopyFile(Path source, Path destination, CopyOption... options)
copy File
if (source == null) {
    throw new IllegalArgumentException("Source cannot be null.");
if (destination == null) {
    throw new IllegalArgumentException("Destination cannot be null.");
if (!Files.exists(source)) {
    throw new FileNotFoundException(source.toString());
...
voidcopyFile(Path source, Path target)
copy File
if (!target.toFile().exists()) {
    Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
} else if (source.toFile().lastModified() != target.toFile().lastModified()) {
    Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
voidcopyFile(Path source, Path target)
Copies the file from source to target.
try {
    Files.copy(source, target);
} catch (Exception ex) {
    throw new RuntimeException(
            "Can not copy the source file " + source.toString() + " to target " + target.toString(), ex);
voidcopyFile(Path source, Path target, boolean foreign, CopyOption... options)
Copy a file to a local or foreign file system
if (!foreign) {
    source.getFileSystem().provider().copy(source, target, options);
    return;
try (InputStream in = Files.newInputStream(source)) {
    Files.copy(in, target);
voidcopyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)
Copy source file to target location.
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);
voidcopyFile(Path source, Path target, boolean prompt, boolean preserve)
CopyUtility source file to target location.
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);