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 Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Boolean compare() method in Java with Examples The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It 2 min read Java Arrays compare() Method with Examples The Arrays compare() method in Java is a part of the java.util package to compare arrays lexicographically (dictionary order). This method is useful for ordering arrays and different overloads for different types including boolean, byte, char, double, float, int, long, short, and Object arrays. Exam 3 min read Java Guava | Chars.compare() method with Examples Chars.compare() method of Guava's Chars Class is used to compare the two specified char values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compa 2 min read Year compareTo() method in Java with Examples The compareTo() method of Year class in Java is used to compare this Year object with another Year object. The comparison of Year object is based on the values of Year.Syntax: public int compareTo(Year otherYear) Parameter: This method accepts a single parameter otherYear. It is the Year object whic 2 min read Like