Java判断一个文件是否是链接文件

来源:互联网 发布:图书管理信息系统java 编辑:程序博客网 时间:2024/04/29 16:12

Apache使用这样的方式来判断:

public static boolean isSymlink(File file) throws IOException {if (file == null)throw new NullPointerException("File must not be null");File canon;if (file.getParent() == null) {canon = file;} else {File canonDir = file.getParentFile().getCanonicalFile();canon = new File(canonDir, file.getName());}return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());}
这种判断方式的依据就是,如果是链接文件(软连接、硬链接),那么getCanonicalFile和getAbsoluteFile的返回值是不一样的。否则一样。

原创粉丝点击