android: determining a symbolic link
I am scanning all directories starting from "/" to find some particular directories like "MYFOLDER". However, the folder is that I get double instances of the same folder. This occurs because one folder is located in "/mnt/sdcard/MYFOLDER" and the same folder 开发者_开发技巧has a symbolic link in "/sdcard/MYFOLDER"..
My Question is, "Is there any way to determine whether the folder is a symbolic link or not?". Please give me some suggestions..
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());
}
精彩评论