Java Copy Directory copyDirectoryContents(File src, File target)

Here you can find the source of copyDirectoryContents(File src, File target)

Description

copy Directory Contents

License

Apache License

Declaration

public static boolean copyDirectoryContents(File src, File target) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class Main {
    public static boolean copyDirectoryContents(File src, File target) {
        Preconditions.checkArgument(src.isDirectory(), "Source dir is not a directory: %s", src);

        // Don't delete symbolic link directories
        if (isSymbolicLink(src)) {
            return false;
        }//w w w . j av  a2  s.com

        target.mkdirs();
        Preconditions.checkArgument(target.isDirectory(), "Target dir is not a directory: %s", src);

        boolean success = true;
        for (File file : listFiles(src)) {
            success = copyRecursively(file, new File(target, file.getName())) && success;
        }
        return success;
    }

    public static boolean isSymbolicLink(File file) {
        try {
            final File canonicalFile = file.getCanonicalFile();
            final File absoluteFile = file.getAbsoluteFile();
            final File parentFile = file.getParentFile();
            // a symbolic link has a different name between the canonical and absolute path
            return !canonicalFile.getName().equals(absoluteFile.getName()) ||
            // or the canonical parent path is not the same as the file's parent path,
            // provided the file has a parent path
                    parentFile != null && !parentFile.getCanonicalPath().equals(canonicalFile.getParent());
        } catch (IOException e) {
            // error on the side of caution
            return true;
        }
    }

    public static ImmutableList<File> listFiles(File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            return ImmutableList.of();
        }
        return ImmutableList.copyOf(files);
    }

    public static ImmutableList<File> listFiles(File dir, FilenameFilter filter) {
        File[] files = dir.listFiles(filter);
        if (files == null) {
            return ImmutableList.of();
        }
        return ImmutableList.copyOf(files);
    }

    public static boolean copyRecursively(File src, File target) {
        if (src.isDirectory()) {
            return copyDirectoryContents(src, target);
        } else {
            try {
                Files.copy(src, target);
                return true;
            } catch (IOException e) {
                return false;
            }
        }
    }
}

Related

  1. copyDirectory(String srcDir, String destDir)
  2. copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir)
  3. copyDirectoryContent(File srcPath, File targetPath)
  4. copyDirectoryContents(File directory, String targetDirName, boolean update)
  5. copyDirectoryContents(File source, File destination)
  6. copyDirectoryContents(File srcDir, File dstDir)
  7. copyDirectoryFromJar(String jarName, String srcDir, File tmpDir)
  8. copyDirectoryTree(File copyFrom, File copyTo)
  9. copyDirectoryWithContents(final File srcPath, final File dstPath)