android:确定符号链接

问题描述 投票:8回答:1

我正在扫描从“/”开始的所有目录,以找到一些特定的目录,如“MYFOLDER”。但是,该文件夹是我获得同一文件夹的双重实例。发生这种情况是因为一个文件夹位于“/ mnt / sdcard / MYFOLDER”中且同一文件夹中的符号链接位于“/ sdcard / MYFOLDER”中。

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

android directory symlink
1个回答
14
投票

这基本上是他们在Apache Commons(受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评论。以上代码仅检查子文件是否为符号链接。

为了回答OP问题,它更容易:

public static boolean containsSymlink(File file) {
  return !file.getCanonicalFile().equals(file.getAbsoluteFile());
}
© www.soinside.com 2019 - 2024. All rights reserved.