File compareTo(File) in Java

compareTo(File): This method is available in the java.io.File class of Java.

Syntax:

int java.io.File.compareTo(File pathname)

This method takes one argument. This method compares two abstract pathnames lexicographically. The ordering is defined by This method depending on the underlying system.

Note:

On UNIXsystems, the alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.

Parameters: One parameter is required for this method.

pathname: The abstract pathname to be compared to this abstract pathname.

Returns: Zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument.

Exceptions: NA

Approach

Java

import java.io.File;

public class FilecompareTo {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        File other = new File(pathname);

        System.out.println(file.compareTo(other));
    }
}

Output:

0


No comments:

Post a Comment