Java File Class compareTo() Method with Examples Last Updated : 02 Feb, 2022 Comments Improve Suggest changes Like Article Like Report The compareTo() function compares two pathnames lexicographically. In Java, the method may be used to sort files. This type of activity is dependent on the system on which JVM is installed. When comparing pathnames on Unix systems, the alphabetic case matters, while it doesn't on Windows. Syntax: public int compareTo(File pathname) Parameters: pathname - The abstract pathname against which this abstract pathname will be compared. Returns: This method returns 0 if the argument is equal to this abstract pathname, a negative value if the abstract pathname is lexicographically less than the argument, and a value larger than 0 if the abstract pathname is lexicographically greater than the argument. Example: Java // Java program to demonstrate the working // of compareTo() method of File class import java.io.File; public class GFG { public static void main(String[] args) { File f1 = new File("c:\\GEEKSFORGEEKS\\Gfg1.txt"); File f2 = new File("c:\\GEEKSFORGEEKS\\Gfg2.txt"); int value = f1.compareTo(f2); if (value == 0) { System.out.println("Both files are equal"); } else if (value > 0) { System.out.println(" Gfg1 is greater than Gfg2"); } else { System.out.println(" Gfg2 is greater than Gfg1"); } } } Output: Comment More infoAdvertise with us Next Article Java File Class compareTo() Method with Examples S sanketnagare Follow Improve Article Tags : Java Java-Functions Java-File Class Practice Tags : Java Similar Reads Charset compareTo() method in Java with Examples The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second w 1 min read Calendar compareTo() Method in Java with Examples The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type an 2 min read Boolean compareTo() method in Java with examples The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return 2 min read Byte compareTo() method in Java with examples The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax ByteObject.compareTo(Byte a) Parameters: It takes a Byte type object a as input which is to be compared with the ins 2 min read Java File Class equals() Method with Examples The equals() method of Java File Class compares the pathname supplied in the argument to the pathname provided in the argument. If the parameter is not null and points to the same file or directory, this function returns true. The operating system determines if the two abstract pathnames are equival 2 min read Like