机器人:确定一个符号链接机器人、符号、链接

2023-09-06 03:58:50 作者:幸福的距離

我是从扫描/找一些特定的目录,如MyFolder的启动所有目录。然而,该文件夹是,我得到相同的文件夹中的双实例。这是因为一个文件夹位于到/ mnt / SD卡/ MyFolder中,并且在同一文件夹中有一个符号链接中的/ SD卡/ MyFolder的..

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..

推荐答案

这是本质上他们是怎么做到的 Apache的百科全书(如有他们的许可证):

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());
}

修改感谢@LarsH评论。 上述code只检查孩子文件是否是一个符号链接。

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());
}