I want to read the contents of a symlink in Java without following the symlink. I know that there are many functions in Files
that allow for the LinkOption.NOFOLLOW_LINKS
option, including Files.copy(symlinkpath, newsymlinkpath, LinkOption.NOFOLLOW_LINKS)
, however, it looks like input streams like FileInputStream
don't have this same option (it will try to follow symlinks by default).
It seems like it should be easy to read the byte representation of a symlink into a buffer in Java, but I can't seem to figure it out.
java.nio.file.Files has readSymbolicLink which does what you want.
Reads the target of a symbolic link (optional operation).
If the file system supports symbolic links then this method is used to read the target of the link, failing if the file is not a symbolic link. The target of the link need not exist. The returned Path object will be associated with the same file system as link.
Example:
try {
Path file = Files.readSymbolicLink(link);
} catch (IOException x) {
System.err.println(x);
}