tags:

views:

47

answers:

1

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.

+3  A: 

Are you restricted to Java versions <= 6?

Otherwise you may want to look at http://java.sun.com/docs/books/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.

aioobe
+1 there is no way to do this in older versions of Java
Romain Hippeau
Thanks for the comments aioobe, Romain. Unfortunately, I am limited to Java 6 otherwise I'd be willing to try out the pre-release JDK 7.
ssahmed555