java.io.File compareTo(File pathname)

Description

On this document we will be showing a java example on how to use the compareTo(File pathname) method of File Class. This method is basically a way to compare two files if they are lexicographically equal. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.

Method Syntax

public int compareTo(File pathname)

Method Argument

Data Type Parameter Description
File pathname The abstract pathname to be compared to this abstract pathname

Method Returns

This method returns boolean, 0 if the argument is equal to this abstract pathname, a value less than zero (which means negative number) 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

Compatibility

Requires Java 1.2 and up

Java File compareTo(File pathname) Example

Below is a java code demonstrates the use of compareTo(File pathname) method of File class. The example presented might be simple however it shows the behaviour of the compareTo(File pathname) method. Basically we instantiated two files and then compare both of them using the compareTo() method. The results were then evaluated using if conditions.

Sample Output

Below is the sample output when you run the above example.

java lang File compareTo() example output

Suggested Reading List

References