Android:确定符号链接



我正在扫描从"/"开始的所有目录,以找到一些特定的目录,如"MYFOLDER"。然而,文件夹是我得到同一文件夹的两个实例。这是因为一个文件夹位于"/mnt/sdcard/MYFOLDER"中,而同一文件夹在"/sdcard/MYFOLDER"中有一个符号链接。

我的问题是,"有没有办法确定文件夹是否为符号链接?"请给我一些建议。

这是他们在Apache Commons中所做的(受制于他们的许可证):

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评论。上面的代码只检查子文件是否为符号链接。

要回答OP的问题,就更容易了:

public static boolean containsSymlink(File file) {
  return !file.getCanonicalFile().equals(file.getAbsoluteFile());
}

最新更新