简体   繁体   English

获取实际文件,给出java6上的符号链接

[英]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. 我能做到,如果我能找到符号链接所指向的实际filePath

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. 我知道如果我们使用Java 7,我们可以使用NIO2的类(即PathPathsFiles )来完成它,但由于几个原因我们无法升级到JDK7。

The same thing I do not see in Java 6. What should be the work around for the same? 我在Java 6中没有看到同样的事情。同样的解决方法应该是什么? I have already tried going through this but it didn't help. 我已经尝试过这个,但它没有帮助。

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. 这个程序演示了getCanonicalPath(),它确实解决了符号链接问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM