Java Path Copy nio copyRecursively(File src, File dest)

Here you can find the source of copyRecursively(File src, File dest)

Description

copy Recursively

License

Open Source License

Declaration

public static void copyRecursively(File src, File dest) throws IOException 

Method Source Code


//package com.java2s;
// The MIT License (MIT)

import java.io.File;
import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void copyRecursively(File src, File dest) throws IOException {
        copyRecursively(src.toPath(), dest.toPath());
    }/*from  ww w  .ja  v a  2 s.c om*/

    public static void copyRecursively(Path src, Path dest) throws IOException {
        BasicFileAttributes srcAttr = Files.readAttributes(src, BasicFileAttributes.class);

        if (srcAttr.isDirectory()) {
            Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    Files.createDirectories(dest.resolve(src.relativize(dir)));
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.copy(file, dest.resolve(src.relativize(file)));
                    return FileVisitResult.CONTINUE;
                }
            });
        } else if (srcAttr.isRegularFile()) {
            Files.copy(src, dest);
        } else {
            throw new IllegalArgumentException("src must denote a directory or file");
        }
    }
}

Related

  1. copyFileToArchive(InputStream srcInputStream, Path destPath)
  2. copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
  3. copyRecursive(Path source, Path target, CopyOption... options)
  4. copyRecursively(File fromDirectory, File toDirectory)
  5. copyRecursively(File src, File dest)
  6. copyRecursively(final Path source, final Path destination, final CopyOption... options)
  7. copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  8. copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  9. copyResource(String resourceLocation, Path target)