The compareTo() method lexicographically compares two given pathnames. The method can be used to order files in Java. Note that this kind of operation depends on the system where JVM was installed. In Unix systems, the alphabetic case is significant in comparing pathnames and in Windows it is not.
2. Method signature
- pathname - abstract pathname we want to be compared to pathname specified in File instance
- - if the argument is equal to this abstract pathname,
- < 0 - if this abstract pathname is lexicographically less than the argument,
- > 0 - if this abstract pathname is lexicographically greater than the argument.
3.1. The program that compares file
/tmp/frontbackend.txt with different files and directory
The output will be as following:
In this article, we presented
File.compareTo() a method that can be used to check if given abstract pathname is lexicographically equal, greater or less then abstract pathname from the argument. This method can be used to order files in a directory.