Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a directory while preserving symlinks using Java File/IO APIs

Tags:

java

unix

file-io

Is it possible to copy directory contents using the Java I/O and File-related APIs while preserving existing symlinks? I am working on a tool that needs to perform "directory copy" operations on a variety of UNIX flavours while preserving existing symlinks.

I'd prefer to attempt this using the core Java SE libraries without resorting Runtime.exec/ProcessBuilder to invoke the platform's "/bin/cp" binary. Apache Commons' IOUtils doesn't appear to support this either.

My last resort will be to use Runtime.exec/ProcessBuilder!

UPDATE: I guess I'll be using Runtime.exec/ProcessBuilder to call out to the native "cp" executable since this doesn't seem to be possible using the core Java APIs nor any of the Apache Commons libraries.

like image 549
ssahmed555 Avatar asked Jun 11 '10 17:06

ssahmed555


1 Answers

Are you restricted to Java versions <= 6?

Otherwise you may want to look at http://docs.oracle.com/javase/tutorial/essential/io/links.html, specifically the sections Detecting a Symbolic Link and Finding the Target of a Link.

From The Java NIO.2 File System in JDK 7:

The java.nio.file API has full support for symbolic links based on the long-standing semantics of UNIX symbolic links -- something that Java developers have long requested.

like image 83
aioobe Avatar answered Oct 23 '22 05:10

aioobe