Jhe*_*ico 8 java windows cross-platform
在java中,可以通过比较文件的规范路径和绝对路径来检测Unix环境中的符号链接.但是,这个技巧在Windows上不起作用.如果我执行
mkdir c:\foo
mklink /j c:\bar
Run Code Online (Sandbox Code Playgroud)
从命令行,然后在java中执行以下行
File f = new File("C:/bar");
System.out.println(f.getAbsolutePath());
System.out.println(f.getCanonicalPath());
Run Code Online (Sandbox Code Playgroud)
输出是
C:\bar
C:\bar
Run Code Online (Sandbox Code Playgroud)
是否有任何pre-Java 7方法可以检测Windows中的连接?
在Java 6或更早版本中似乎没有任何跨平台机制,尽管它是一个使用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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:更新每条评论可能的错误返回 getWin32FileAttributes()
| 归档时间: |
|
| 查看次数: |
3096 次 |
| 最近记录: |