Java:检查符号链接文件是否存在

时间:2023-01-07 16:26:01

We talk about java 1.6 here. Since symoblic link is not yet supported, how can examine the existence of them.

我们在这里讨论java 1.6。由于尚不支持symoblic链接,如何检查它们的存在。

1: tell wheather the link file itself exists (return true even if the link is broken)

1:告诉链接文件本身是否存在(即使链接被破坏也返回true)

2: follow the link and tell wheather underlying file exists.

2:跟随链接,告诉底层文件是否存在。

Is there really no way to realize this except JNI?

除了JNI,真的没有办法实现这一点吗?

5 个解决方案

#1


6  

looks that way for now... unless you go with openjdk http://openjdk.java.net/projects/nio/javadoc/java/nio/file/attribute/BasicFileAttributes.html#isSymbolicLink()

现在看来……除非使用openjdk http://openjdk.java.net/projects/nio/javadoc/java/nio/nio/file/attribute/basicfileattributes.html # issymbol clink ()

#2


5  

It is slow but you could compare getAbsolutePath() and getCanonicalPath() of a File. So use only outside a performance critical code path.

它是缓慢的,但是您可以比较一个文件的getAbsolutePath()和getCanonicalPath()。因此,只在性能关键代码路径之外使用。

#3


1  

The following should give you a start:

以下是你的开始:

if (file.exists() && !file.isDirectory() && !file.isFile()) {
    // it is a symbolic link (or a named pipe/socket, or maybe some other things)
}

if (file.exists()) {
    try {
        file.getCanonicalFile();
    } catch (FileNotFoundException ex) {
        // it is a broken symbolic link
    }
}

EDIT : The above don't work as I thought because file.isDirectory() and file.isFile() resolve a symbolic link. (We live and learn!)

编辑:上面的操作不像我想的那样,因为file.isDirectory()和file.isFile()解析了一个符号链接。(我们活到老,学到老!)

If you want an accurate determination, you will need to use JNI to make the relevant native OS-specific library calls.

如果您想要精确的确定,您将需要使用JNI进行相关的本地特定于os的库调用。

#4


1  

hmm..not yet supported..will it ever be supported is the question that comes to my mind looking at this question...symbolic links are platform specific and Java is supposed to b platform independent. i doubt if anything of the sort is possible with java as such..you may have to resort to native code for this portion and use JNI to bridge it with the rest of your program in java.

嗯. .不支持. .当我想到这个问题的时候,我的脑海中就会出现这样一个问题……符号链接是平台特定的,Java应该是b平*立的。我怀疑这类事情在java本身是否可能。您可能需要使用这部分的本地代码,并使用JNI将它与java中的其他程序连接起来。

#5


0  

#2 is easy: just use File.isFile etc., which will traverse the link. The hard part is #1.

第二条很简单:使用文件即可。isFile等,用于遍历链接。最难的部分是第一条。

So if you cannot use java.nio.file, and want to avoid JNI etc., the only thing I found which works:

如果不能使用java.nio。为了避免JNI等文件,我只找到了一件有效的东西:

static boolean exists(File link) {
    File[] kids = link.getParentFile().listFiles();
    return kids != null && Arrays.asList(kids).contains(link);
}

Not terribly efficient but straightforward.

效率不高,但很直接。

#1


6  

looks that way for now... unless you go with openjdk http://openjdk.java.net/projects/nio/javadoc/java/nio/file/attribute/BasicFileAttributes.html#isSymbolicLink()

现在看来……除非使用openjdk http://openjdk.java.net/projects/nio/javadoc/java/nio/nio/file/attribute/basicfileattributes.html # issymbol clink ()

#2


5  

It is slow but you could compare getAbsolutePath() and getCanonicalPath() of a File. So use only outside a performance critical code path.

它是缓慢的,但是您可以比较一个文件的getAbsolutePath()和getCanonicalPath()。因此,只在性能关键代码路径之外使用。

#3


1  

The following should give you a start:

以下是你的开始:

if (file.exists() && !file.isDirectory() && !file.isFile()) {
    // it is a symbolic link (or a named pipe/socket, or maybe some other things)
}

if (file.exists()) {
    try {
        file.getCanonicalFile();
    } catch (FileNotFoundException ex) {
        // it is a broken symbolic link
    }
}

EDIT : The above don't work as I thought because file.isDirectory() and file.isFile() resolve a symbolic link. (We live and learn!)

编辑:上面的操作不像我想的那样,因为file.isDirectory()和file.isFile()解析了一个符号链接。(我们活到老,学到老!)

If you want an accurate determination, you will need to use JNI to make the relevant native OS-specific library calls.

如果您想要精确的确定,您将需要使用JNI进行相关的本地特定于os的库调用。

#4


1  

hmm..not yet supported..will it ever be supported is the question that comes to my mind looking at this question...symbolic links are platform specific and Java is supposed to b platform independent. i doubt if anything of the sort is possible with java as such..you may have to resort to native code for this portion and use JNI to bridge it with the rest of your program in java.

嗯. .不支持. .当我想到这个问题的时候,我的脑海中就会出现这样一个问题……符号链接是平台特定的,Java应该是b平*立的。我怀疑这类事情在java本身是否可能。您可能需要使用这部分的本地代码,并使用JNI将它与java中的其他程序连接起来。

#5


0  

#2 is easy: just use File.isFile etc., which will traverse the link. The hard part is #1.

第二条很简单:使用文件即可。isFile等,用于遍历链接。最难的部分是第一条。

So if you cannot use java.nio.file, and want to avoid JNI etc., the only thing I found which works:

如果不能使用java.nio。为了避免JNI等文件,我只找到了一件有效的东西:

static boolean exists(File link) {
    File[] kids = link.getParentFile().listFiles();
    return kids != null && Arrays.asList(kids).contains(link);
}

Not terribly efficient but straightforward.

效率不高,但很直接。