Discuss JDK file.equal ()

Source: Internet
Author: User

We generally compare two files with objects in the same file, usually using Java.io.File.equal (). Here, equal () is not the result of a comparison of the file contents. Like whether to point to the same file .

The equal () method of file. The CompareTo () of the current file system filesystem is actually called.

    public boolean equals (Object obj) {        if (obj! = null) && (obj instanceof File) {            return compareTo (file) o BJ) = = 0;        }        return false;    }    static private FileSystem fs = Filesystem.getfilesystem ();    public int CompareTo (File pathname) {        return Fs.compare (this, pathname);    }


We find that there is no implementation of Unix/linux in Java.io.FileSystem, only Win32filesystem, so it is the implementation class that is called by default. Its comparison to the file, in fact, is the file name and absolute path comparison.

Assuming that two file objects have the same getpath (), they think they are the same file. And it can be seen that Windows does not differentiate between uppercase and lowercase .

such as the following Java.io.Win32FileSystem.compare ().

    public int Compare (file f1, file F2) {        return F1.getpath (). Comparetoignorecase (F2.getpath ());    }


This way, you can use the absolute path to test whether two objects point to the same file, the majority of the situation, but also be careful. For example , under Linux , file names are sensitive to uppercase and lowercase, and cannot be ignore . And the file created by the hard link is still pointing to the same file, but it is falsein file.equal () .

Therefore, the tool class Java.nio.file.Files was introduced after JDK1.7, and the Issamefile () can be used to infer whether the two file objects point to the same file.

    public boolean Issamefile (path path, path path2) throws IOException {        return provider (path). Issamefile (path, path2);     }    private static Filesystemprovider provider (path Path) {        return Path.getfilesystem (). Provider ();    

He gets the provider of the current system, and then calls its issamefile () to verify. The implementation hierarchy of the following filesystem:

Java.nio.file.spi.FileSystemProvider

Sun.nio.fs.AbstractFileSystemProvider

Sun.nio.fs.UnixFileSystemProvider

Sun.nio.fs.LinuxFileSystemProvider

Sun.nio.fs.WindowsFileSystemProvider

Let's start by looking at how Unixfilesystemprovider.issamefile () is implemented:

    public boolean issamefile (path obj1, path Obj2) throws IOException {        Unixpath file1 = Unixpath.tounixpath (obj1);        if (File1.equals (OBJ2))            return true;         File1.checkread (); File2.checkread ();        Unixfileattributes attrs1 = Unixfileattributes.get (File1, true);        Unixfileattributes attrs2 = Unixfileattributes.get (File2, true);        Return Attrs1.issamefile (ATTRS2);    }


He first called unixpath.equal (), then checked the readability of the two files, and finally called Unixfileattributes.issamefile ().

It is very clear that he will first check whether the absolute path of two files is the same ( uppercase and lowercase ), assuming that the same file is the same. If different, check the inode number of the two files.

This is the Unix file system characteristics, the file is identified by the inode, only the inode number is the same, it means pointing to the same file.

So it can be used to infer whether two hard links point to the same file.

------------------------Unixpath------------------------

    public boolean equals (Object ob) {        if (ob! = null) && (ob instanceof Unixpath))            return CompareTo ((Path) OB ) = = 0;    Compare both path        return false;    }    public int CompareTo (Path other) {        int len1 = path.length;        int len2 = ((Unixpath) other). Path.length;        int n = math.min (len1, len2);        byte v1[] = path;        byte v2[] = ((Unixpath) other). path;        int k = 0;        while (K < n) {            int c1 = v1[k] & 0xff;            int c2 = v2[k] & 0xff;            if (c1! = C2)                return c1-c2;        }        return len1-len2;    }

------------------------Unixfileattributes------------------------

    Boolean issamefile (Unixfileattributes attrs) {        return (St_ino = = Attrs.st_ino) && (St_dev = Attrs.st_dev)) ;    }


and for Windows systems. It's also very similar, take a look at Windowsfilesystemprovider.issamefile (), windowspath.equal () and Windowsfileattributes.issamefile ().

is to infer the absolute path of the file first ( ignoring uppercase and lowercase ), assuming that equality is the same file, assuming that the underlying inference is not equal. The inference of the windows underlying files is to check that the disk numbers are equal to complete.

------------------------Windowsfilesystemprovider------------------------

    public boolean issamefile (path obj1, path Obj2) throws IOException {        Windowspath file1 = Windowspath.towindowspath (ob J1);        if (File1.equals (OBJ2))            return true;         File1.checkread (); File2.checkread ();        Windowsfileattributes attrs1 =windowsfileattributes.readattributes (H1);         Windowsfileattributes attrs2 =windowsfileattributes.readattributes (H2);        Return Windowsfileattributes.issamefile (attrs1, attrs2);    }

------------------------windowspath------------------------

    public boolean equals (Object obj) {        if (obj! = null) && (obj instanceof windowspath))            return CompareTo (P ATH) = = 0;        return false;    }    public int compareTo (Path obj) {        if (obj = = null)            throw new NullPointerException ();        String S1 = path;        String s2 = ((windowspath) obj). path;        int n1 = S1.length ();        int n2 = S2.length ();        int min = math.min (n1, N2);        for (int i = 0; i < min; i++) {            char C1 = S1.charat (i);            char C2 = S2.charat (i);             if (c1! = C2) {                 C1 = character.touppercase (c1);                 C2 = character.touppercase (C2);                 if (c1! = C2)                     return c1-c2;             }        }        return n1-n2;    }

------------------------Windowsfileattributes------------------------

    Static Boolean Issamefile (Windowsfileattributes attrs1, windowsfileattributes attrs2) {        //volume serial number and F Ile index must be the same        return (Attrs1.volserialnumber = = Attrs2.volserialnumber) &&            ( Attrs1.fileindexhigh = = Attrs2.fileindexhigh) &&            (Attrs1.fileindexlow = = Attrs2.fileindexlow);    }

The comparison is clear. Assume that the absolute path of the file is only equal (not the content). File.equal () can be used with confidence. Instead, assume that you are pointing to the same file in the OS. Ability to use Files.issamefile (). It takes into account the differences in different file systems. At the same time. We can also look at the differences between different OS file systems by observing the different implementations of the two system check rules. If you are interested, you can go further into the study Oh!

Finally, pay the source code address of the previous Openjava, and you can find the very many sun.xxx.xxx sources referenced in the JDK. such as the series of sun.nio.fs.xxx mentioned above. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/awt/shell/ShellFolder.java# Shellfolder.compareto%28java.io.file%29

Discuss JDK file.equal ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.