This is essentially how they do in Apache Commons (subject to their license):
public static boolean isSymlink(File file) throws IOException {
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());
}
Edit thanks to @LarsH comment.
The above code only checks whether the children file is a symlink.
In order to answer the OP question, it's even easier:
public static boolean containsSymlink(File file) {
return !file.getCanonicalFile().equals(file.getAbsoluteFile());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…