Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy entire directory contents to another directory? [duplicate]

Method to copy entire directory contents to another directory in java or groovy?

like image 562
Sanal MS Avatar asked Jun 02 '11 12:06

Sanal MS


People also ask

How can we copy an entire directory under another directory?

Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.

How do I SCP an entire directory?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.

How do I copy content from one folder to another?

To copy the item: click Ctrl+C. Navigate to the folder where you want to move or copy the item to, and then click Ctrl+V.


2 Answers

FileUtils.copyDirectory()

Copies a whole directory to a new location preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

To do so, here's the example code

String source = "C:/your/source"; File srcDir = new File(source);  String destination = "C:/your/destination"; File destDir = new File(destination);  try {     FileUtils.copyDirectory(srcDir, destDir); } catch (IOException e) {     e.printStackTrace(); } 
like image 89
jmj Avatar answered Oct 13 '22 06:10

jmj


The following is an example of using JDK7.

public class CopyFileVisitor extends SimpleFileVisitor<Path> {     private final Path targetPath;     private Path sourcePath = null;     public CopyFileVisitor(Path targetPath) {         this.targetPath = targetPath;     }      @Override     public FileVisitResult preVisitDirectory(final Path dir,     final BasicFileAttributes attrs) throws IOException {         if (sourcePath == null) {             sourcePath = dir;         } else {         Files.createDirectories(targetPath.resolve(sourcePath                     .relativize(dir)));         }         return FileVisitResult.CONTINUE;     }      @Override     public FileVisitResult visitFile(final Path file,     final BasicFileAttributes attrs) throws IOException {     Files.copy(file,         targetPath.resolve(sourcePath.relativize(file)));     return FileVisitResult.CONTINUE;     } } 

To use the visitor do the following

Files.walkFileTree(sourcePath, new CopyFileVisitor(targetPath)); 

If you'd rather just inline everything (not too efficient if you use it often, but good for quickies)

    final Path targetPath = // target     final Path sourcePath = // source     Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {         @Override         public FileVisitResult preVisitDirectory(final Path dir,                 final BasicFileAttributes attrs) throws IOException {             Files.createDirectories(targetPath.resolve(sourcePath                     .relativize(dir)));             return FileVisitResult.CONTINUE;         }          @Override         public FileVisitResult visitFile(final Path file,                 final BasicFileAttributes attrs) throws IOException {             Files.copy(file,                     targetPath.resolve(sourcePath.relativize(file)));             return FileVisitResult.CONTINUE;         }     }); 
like image 44
Archimedes Trajano Avatar answered Oct 13 '22 06:10

Archimedes Trajano