zoukankan      html  css  js  c++  java
  • java、android 对比两个目录或文件是否是同一个目录或文件的方法

    由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1      /storage/ext_sdcard ,如果通过某种方式(如mount命令)得到了这两个路径,但是现在要给路径去重,可以采用如下方法:

    一、首先,要判断目录是否是同一个目录,可能会用到判断一个文件是否是同一个文件的方法,如果已经得到文件的路径 filePath1 和 filePath2,则可以这样来对比是否是同一个物理文件:

    1、先检查这两个文件是否是符号链接,若是符号链接,通过采用File.getCanonicalPath() 的方法得到文件的正规路径(Canonical是规范的、正规的、正则的意思):

    检查文件是否是符号链接:

    // 自己补上: 先检查文件是否存在,不存在则直接返回 false
    public
    static boolean isSymlink(File file) throws IOException { if (file == null) throw new NullPointerException("File must not be null"); 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()); }

    如果是符号链接,采用File.getCanonicalPath()的方法得到文件的规范路径:

    // 自己加上:先检查文件是否存在,不存在则直接返回 file.getAbsolutePath() 或者 a.getPath()
    if (isSymlink(file)) {
      return file.getCanonicalFile();
    }

    关于canonical pathname的解释:

    A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

    2、得到文件的canonical path之后,对比文件的inode是否相等:

    Runtime.getRuntime().exec("ls -i" + " " + file_canonical_path);

    然后得到输出后先trim,再split(" "),再判断获得的第一个元素是否全是数字,如果全是数字则是其inode。

    二、判断两个目录(A和B)是否是同一个目录:

     1、先通过查找某个文件夹目录(如A)里的第一个文件,找到其相对路径:

    public void listFilesForFolder(final File folder) {
    if (!folder.exists()) return;

    for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } }

    上面的程序可以找到某文件夹里的所有文件,自己取某些文件即可。

    得到该文件夹(A)的某些文件之后,通过获得这些文件的 AbsolutePath 字符串,然后再从 这些 AbsolutePath 字符串里去掉 文件夹A的 AbsolutePath 字符串,就得到了这些文件的相对路径。注意,建议别用网上找到的方法来获取相对路径(因为有些时候 toURI() 会改变文件的路径,由于某些符号映射机制,如我的 android手机(华为荣耀7) 里的 /storage/sdcard1 就被映射成了 /storage/0123-4567,所有/storage/sdcard1目录下的文件,调用 toURI()之后都被改变了根目录为 /storage/0123-4567 ):

    ring path = "/var/data/stuff/xyz.dat";
    String base = "/var/data";
    String relative = new File(base).toURI().relativize(new File(path).toURI()).getPath();     # 不要用这种方法获取相对路径

    除了上面得到相对路径的方法外,还有一种更好的方法得到第一个文件的相对路径,建议用下面这种方法得到相对路径:

    public void listFilesForFolder(final File folder) {
        if (!folder.exists()) return;
    
        String relativePath = "";
    String fileName = "";
    for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) {
    relativePath += fileEntry.getName() + "/"; listFilesForFolder(fileEntry); }
    else {
    fileNmae = fileEntry.getName(); System.out.println(fileEntry.getName());
    break; } } }

    2、得到文件夹A里某些文件的相对路径之后,在B文件夹里创建这些文件,即new File(B, fileRelativePath), 将这些文件与文件夹A的相应的文件进行对比,看看他们的inode是否相同。相同则表明A和B是同一个文件夹,如果不同,则不是同一个文件夹。方法如下:

    假如从文件夹A得到里一个文件 dir/file1.txt,通过字串方式去掉前面A的路径,自然语言描述如下:

    File file1A = new File("A", "dir/file1.txt");

    File file1B = new File("B", file1RelativePath);

    if (!file1B.exists()) 则A和B不是同一个目录;

    if (file1B的inode等于file1A的inode)则是同一个目录,否则不是。获取inode的时候不用将符号链接变成Canonical Path,即不推荐将两个文件都调用 getCanonicalFile(),因为如果A和B是同一个文件夹,则它里面的文件(哪怕是符号链接文件)也应该是同一个文件。

  • 相关阅读:
    PAT-乙级-1011. A+B和C (15)
    PAT-乙级-1010. 一元多项式求导 (25)
    PAT-乙级-1009. *说反话 (20)
    PAT-乙级-1008. 数组元素循环右移问题 (20)
    PAT-乙级-1007. 素数对猜想 (20)
    PAT-乙级-1006. 换个格式输出整数 (15)
    PAT-乙级-1005. 继续(3n+1)猜想 (25)
    PAT-乙级-1004. 成绩排名 (20)
    BZOJ 1030: [JSOI2007]文本生成器
    BZOJ 2938: [Poi2000]病毒
  • 原文地址:https://www.cnblogs.com/welhzh/p/5990822.html
Copyright © 2011-2022 走看看