java_enthu
java_enthu

Reputation: 2327

Getting the actual file, given the Symbolic Link on java6

I've a directory on remote machine in which I have symbolic links pointing to actual files, and I am supposed to read the actual files given the symbolic link.

I can do it, if I can find the actual filePath, which is pointed to by the Symbolic Link.

I know that if we were using Java 7 we could do it using NIO2's classes (namely Path, Paths and Files), but we can't upgrade to JDK7 for several reasons.

The same thing I do not see in Java 6. What should be the work around for the same? I have already tried going through this but it didn't help.

Upvotes: 3

Views: 1337

Answers (1)

Jon B
Jon B

Reputation: 481

I don't have enough reputation to comment, but I can give an answer. Strange.

This program demonstrates getCanonicalPath() and it does indeed resolve the symlink.

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        File link = new File("testlink");
        System.out.println(link.getCanonicalPath());
    }
}

and running it produces:

[jon@dragon ~]$ ls -l testlink
lrwxrwxrwx. 1 jon jon 9 Apr 15 13:31 testlink -> Test.java
[jon@dragon ~]$ java Test
/home/jon/Test.java

Upvotes: 3

Related Questions