android:确定符号链接 [英] android: determining a symbolic link

查看:40
本文介绍了android:确定符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扫描从/"开始的所有目录,以找到一些特定的目录,例如MYFOLDER".但是,该文件夹是我获得同一文件夹的双重实例.出现这种情况是因为一个文件夹位于/mnt/sdcard/MYFOLDER"中,而同一文件夹在/sdcard/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 Commons(受他们的许可证):

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

Edit thanks to @LarsH comment. The above code only checks whether the children file is a symlink.

为了回答 OP 问题,就更简单了:

In order to answer the OP question, it's even easier:

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

这篇关于android:确定符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆