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

voidcopyDir(File dir1, File dir2)
copy Dir
if (!dir1.isDirectory())
    throw new IOException("Not a directory: " + dir1);
if (dir2.exists())
    throw new IOException("Cannot copy to existing directory: " + dir2);
dir2.mkdirs();
File[] files = dir1.listFiles();
if (files != null) {
    for (File f : files) {
...
voidcopyDir(File sourceDir, File targetDir)
copy Dir
if (targetDir.exists()) {
    if (!targetDir.isDirectory()) {
        throw new IOException("Not a directory " + targetDir.getAbsolutePath());
} else if (!targetDir.mkdirs()) {
    throw new IOException("Failed to create directory " + targetDir.getAbsolutePath());
File[] children = sourceDir.listFiles();
...
voidcopyDir(final Path fromPath, final Path toPath)
copy Dir
toPath.toFile().mkdirs();
Files.walkFileTree(fromPath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path targetPath = toPath.resolve(fromPath.relativize(dir));
        if (!Files.exists(targetPath)) {
            Files.createDirectory(targetPath);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.copy(file, toPath.resolve(fromPath.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
        return FileVisitResult.CONTINUE;
});
voidcopyDir(final String src, final String dest)
copy Dir
copyDir(new File(src), new File(dest));
voidcopyDir(Path from, Path to, IProgressMonitor monitor)
copy Dir
Files.walkFileTree(from, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
        new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                    throws IOException {
                Path targetdir = to.resolve(from.relativize(dir));
                try {
                    Files.copy(dir, targetdir);
...
voidcopyDir(String sourceDir, String targetDir)
copy files from directory sourceDirOrFile to directory targetDirOrFile
File sDir = new File(sourceDir);
File tDir = new File(targetDir);
if (!tDir.exists()) {
    tDir.mkdirs();
if (sDir.exists()) {
    if (sDir.isDirectory()) {
        File[] files = sDir.listFiles();
...
voidcopyDir(String src, String dst)
copy Dir
copyDir(new File(src), new File(dst));
voidcopyDirectiory(String sourceDir, String targetDir)
copy Directiory
(new File(targetDir)).mkdirs();
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
    if (file[i].isFile()) {
        File sourceFile = file[i];
        File targetFile = new File(
                new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
        copyFile(sourceFile, targetFile);
...
voidcopyDirectory(File in, File out)
copy Directory
copyDirectory(in, out, Collections.<String>emptySet());
voidcopyDirectory(File in, File out)
copy Directory
copyDirectory(in, out, Collections.<String>emptySet());