java.io.File compareTo(File pathname)
Description
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.
package com.javatutorialhq.java.examples; import java.io.File; /* * This example source code demonstrates the use of * compareTo(File pathname) method of File class. * */ public class FileCompareToExample { public static void main(String[] args) { // initialize File object File firstFile = new File("C:javatutorialhqtest1.txt"); File secondFile = new File("C:javatutorialhqtest2.txt"); int i = firstFile.compareTo(secondFile); if(i==0){ System.out.println("they are equal"); } if(i>0){ System.out.println("First file is greater than the second file"); } if(i<0){ System.out.println("Second file is greater than the first file"); } } }