Java File.compareTo(File pathname)

Syntax

File.compareTo(File pathname) has the following syntax.

public int compareTo(File pathname)

Example

In the following code shows how to use File.compareTo(File pathname) method.


/*  ww  w  .  j  a  v  a  2  s .  com*/

import java.io.File;

public class Main {
  public static void main(String[] args) {

    File f = new File("test.txt");
    File f1 = new File("File/test1.txt");

    // returns integer value
    int value = f.compareTo(f1);

    if (value == 0) {
      System.out.print(" = ");
    }
    else if (value > 0) {
      System.out.print(" >  ");
    }
    else {
      System.out.print(" < ");
    }
    System.out.print("Value returned: " + value);

  }
}

The code above generates the following result.