Java Utililty Methods Directory Copy nio

List of utility methods to do Directory Copy nio

Description

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

Method

voidcopyDirectory(File source, File dest, CopyOption... options)
https://stackoverflow.com/questions/11651900/how-to-recursively-copy-entire-directory-including-parent-folder-in-java
if (!dest.exists())
    dest.mkdirs();
File[] contents = source.listFiles();
if (contents != null) {
    for (File f : contents) {
        File newFile = new File(dest.getAbsolutePath() + File.separator + f.getName());
        if (f.isDirectory())
            copyDirectory(f, newFile, options);
...
voidcopyDirectory(File source, File destination)
copy Directory
if (!source.exists()) {
    throw new IllegalArgumentException("Source directory (" + source.getPath() + ") doesn't exist.");
if (!source.isDirectory()) {
    throw new IllegalArgumentException("Source (" + source.getPath() + ") must be a directory.");
destination.mkdirs();
File[] files = source.listFiles();
...
voidcopyDirectory(File source, File destination)
copy Directory
if (!source.isDirectory()) {
    throw new IllegalArgumentException("Source (" + source.getPath() + ") must be a directory.");
if (!source.exists()) {
    throw new IllegalArgumentException("Source directory (" + source.getPath() + ") doesn't exist.");
if (destination.exists()) {
    throw new IllegalArgumentException("Destination (" + destination.getPath() + ") exists.");
...
voidcopyDirectory(File source, File destination)
Recursively copies a directory from source to destination including all the files within the folders
if (!source.isDirectory()) {
    throw new IllegalArgumentException("Source (" + source.getPath() + ") must be a directory.");
if (!source.exists()) {
    throw new IllegalArgumentException("Source directory (" + source.getPath() + ") doesn't exist.");
destination.mkdirs();
File[] files = source.listFiles();
...
voidcopyDirectory(File source, File destination, boolean recursive)
Copies a source directory to a destination directory.
if (!source.exists())
    throw new FileNotFoundException("Source not found: " + source);
if (!source.canRead())
    throw new IOException("Source is unreadable: " + source);
if (!source.isDirectory())
    throw new IOException("Source is not a directory: " + source);
if (destination.exists() && !destination.canWrite())
    throw new IOException("Destination is unwritable: " + destination);
...
voidcopyDirectory(File source, File target, boolean acceptFileInUseError)
copy Directory
if (source.getName().equalsIgnoreCase(".svn")) {
    return;
if (!target.exists()) {
    boolean success = target.mkdirs();
    if (!success) {
        throw new RuntimeException(
                "BBUtils.copyDirectory: could not create directory " + target.getAbsolutePath());
...
voidcopyDirectory(File sourceDir, File destinationDir)
Copies the contents of one directory into the other.
if (sourceDir == null || !sourceDir.exists() || destinationDir == null) {
    return;
destinationDir.mkdirs();
File[] files = sourceDir.listFiles();
if (files == null) {
    files = new File[0];
for (File sourceFile : files) {
    if (sourceFile.isDirectory()) {
        File newDestSubDir = new File(destinationDir, sourceFile.getName());
        newDestSubDir.mkdirs();
        newDestSubDir.setLastModified(sourceFile.lastModified());
        copyDirectory(sourceFile, newDestSubDir);
    } else {
        File destFile = new File(destinationDir, sourceFile.getName());
        copyFile(sourceFile, destFile);
        destFile.setLastModified(sourceFile.lastModified()); 
voidcopyDirectory(File sourceDir, File targetDir)
copy Directory
if (sourceDir.isDirectory()) {
    copyDirectoryRecursively(sourceDir.getAbsoluteFile(), targetDir.getAbsoluteFile());
} else {
    Files.copy(sourceDir.getAbsoluteFile().toPath(), targetDir.getAbsoluteFile().toPath(),
            java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
voidcopyDirectory(File sourceDirectory, File targetDirectory)
Copies directories, preserving file attributes.
final Path sourcePath = Paths.get(sourceDirectory.toURI()).toRealPath();
final Path targetPath = Paths.get(targetDirectory.toURI());
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path relative = sourcePath.relativize(dir);
        Path target = targetPath.resolve(relative);
        File folder = target.toFile();
...
voidcopyDirectory(File sourceFile, File destFile)
copy Directory
if (sourceFile.isDirectory()) {
    if (!destFile.exists()) {
        destFile.mkdir();
    String files[] = sourceFile.list();
    for (int i = 0; i < files.length; i++) {
        copyDirectory(new File(sourceFile, files[i]), new File(destFile, files[i]));
} else {
    if (!sourceFile.exists()) {
    } else {
        copyFile(sourceFile, destFile);