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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2175673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:52:40  来源:igfitidea点击:

Java: check symbolic link file existence

javasymlink

提问by solotim

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

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

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,真的没有办法实现这一点吗?

回答by user159313

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

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

回答by Jesse Glick

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

#2 很简单:只需使用File.isFileetc.,它将遍历链接。困难的部分是#1。

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

因此,如果您不能使用java.nio.file,并且想避免使用 JNI 等,我发现唯一有效的方法是:

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

Not terribly efficient but straightforward.

效率不高,但很直接。

回答by Stephen C

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
    }
}
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 来进行相关的本地操作系统特定的库调用。

回答by Aadith Ramia

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 应该是独立于平台的。我怀疑 java 是否可以进行任何此类操作..您可能不得不为此部分使用本机代码并使用 JNI 将其与 java 中的程序的其余部分桥接。