Java Directory Copy nio copyDir(String src, String dst)

Here you can find the source of copyDir(String src, String dst)

Description

copy Dir

License

Apache License

Declaration

public static void copyDir(String src, String dst) 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
    public static void copyDir(String src, String dst) {
        copyDir(new File(src), new File(dst));
    }//from w  ww .ja  v  a  2 s  . c  o m

    public static void copyDir(File src, File dst) {
        if (src.isDirectory()) {
            dst.mkdirs();

            File[] files = src.listFiles();
            if (files != null)
                for (File i : files) {
                    File j = new File(dst.getAbsolutePath() + File.separatorChar + i.getName());
                    if (i.isDirectory())
                        copyDir(i, j);
                    else
                        copyFile(i, j);
                }
        }
    }

    public static void copyFile(File src, File dst) {
        try {
            Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.COPY_ATTRIBUTES,
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException x) {
            x.printStackTrace();
        }
    }
}

Related

  1. copyDir(File sourceDir, File targetDir)
  2. copyDir(final Path fromPath, final Path toPath)
  3. copyDir(final String src, final String dest)
  4. copyDir(Path from, Path to, IProgressMonitor monitor)
  5. copyDir(String sourceDir, String targetDir)
  6. copyDirectiory(String sourceDir, String targetDir)
  7. copyDirectory(File in, File out)
  8. copyDirectory(File in, File out)
  9. copyDirectory(File source, File dest, CopyOption... options)