Java File compareTo(File pathname) method example

HQ ┬╗ Java Tutorial ┬╗ java.io ┬╗ File ┬╗ compareTo() example output
Java File compareTo(File pathname) method example 2018-02-16T07:26:44+00:00

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

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close