跨平台方式检测符号链接/连接点? [英] Cross platform way to detect a symbolic link / junction point?
本文介绍了跨平台方式检测符号链接/连接点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在java中,可以通过比较文件的规范路径和绝对路径来检测Unix环境中的符号链接。但是,这个技巧在Windows上不起作用。如果我执行
In java, a symbolic link in a Unix environment can be detected by comparing the file's canonical and absolute path. However, this trick does not work on windows. If I execute
mkdir c:\foo
mklink /j c:\bar
然后在java中执行以下行
from the command line and then execute the following lines in java
File f = new File("C:/bar");
System.out.println(f.getAbsolutePath());
System.out.println(f.getCanonicalPath());
输出
C:\bar
C:\bar
是有没有在Java中检测结点的Java前方法?
Is there any pre-Java 7 way of detecting a junction in windows?
推荐答案
似乎没有任何跨平台机制对于Java 6或更早版本中的这个,虽然它是一个使用JNA的相当简单的任务
There doesn't appear to be any cross platform mechanism for this in Java 6 or earlier, though its a fairly simple task using JNA
interface Kernel32 extends Library {
public int GetFileAttributesW(WString fileName);
}
static Kernel32 lib = null;
public static int getWin32FileAttributes(File f) throws IOException {
if (lib == null) {
synchronized (Kernel32.class) {
lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
}
}
return lib.GetFileAttributesW(new WString(f.getCanonicalPath()));
}
public static boolean isJunctionOrSymlink(File f) throws IOException {
if (!f.exists()) { return false; }
int attributes = getWin32FileAttributes(f);
if (-1 == attributes) { return false; }
return ((0x400 & attributes) != 0);
}
编辑:根据的可能错误返回的每条评论更新getWin32FileAttributes()
这篇关于跨平台方式检测符号链接/连接点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文