Compares one File object with another object.
Package: java.io
Assembly: vjslib (in vjslib.dll)
public int compareTo(
java.lang.Object o);
Parameters
- o
-
An object representing a file or folder to compare against.

Return Value
Less than zero if the path and name of the object calling compareTo is alphabetically less than the object o, 0 if the path and name of the object calling compareTo is alphabetically equal to the object o, and greater than zero if the path and name of the object calling compareTo is alphabetically greater than the object o.

Example
The following example demonstrates how to compare two file names alphabetically.
// file_compareto_2.jsl
import java.io.*;
public class Program
{
public static void main(String[] args)
{
// Open the file C:\MyFile.txt.
Object myFile = new File("C:\\MyFile.txt");
// Open the file C:\YourFile.txt.
Object yourFile = new File("C:\\YourFile.txt");
if (myFile instanceof File &&
yourFile instanceof File &&
((File)myFile).exists() && ((File)yourFile).exists())
{
// Compare the two files alphabetically.
int compareResult = ((File)myFile).compareTo(yourFile);
// Display the results of the comparison.
if (compareResult < 0)
{
System.out.println(((File)myFile).getAbsolutePath() +
" is less than " +
((File)yourFile).getAbsolutePath());
}
else if (compareResult == 0)
{
System.out.println(((File)myFile).getAbsolutePath() +
" is equal to " +
((File)yourFile).getAbsolutePath());
}
else
{
System.out.println(((File)myFile).getAbsolutePath() +
" is greater than " +
((File)yourFile).getAbsolutePath());
}
}
}
}
/*
Output:
C:\MyFile.txt is less than C:\YourFile.txt
*/

See Also