Java - File compareTo() Method



Description

The Java File compareTo(File pathname) method compares two abstract pathnames lexicographically. The ordering defined by this method is dependent upon the operating system.

Declaration

Following is the declaration for java.io.File.compareTo(File pathname) method −

public int compareTo(File pathname)

Parameters

pathname − The abstract pathname to be compared to the this abstract pathname.

Return Value

This method returns Zero if the argument is equal to this abstract pathname, negative value and a value greater than 0 if the abstract pathname is lexicographically less than the argument and greater than the argument respectively.

Exception

NA

Example 1

The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test1.txt file. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Lastly we're printing the result of comparison.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File f1 = null;
      
      try {
         // create new files
         f = new File("File/test.txt");
         f1 = new File("File/test1.txt");
         
         // returns integer value
         int value = f.compareTo(f1);
         
         // prints
         System.out.print("Lexicographically, ");
         System.out.print("abstract path name test.txt");
         
         // if lexicographically, argument = abstract path name
         if(value == 0) {
            System.out.print(" = ");
         }
         
         // if lexicographically, argument < abstract path name
         else if(value > 0) {
            System.out.print(" > ");
         }
         
         // if lexicographically, the argument > abstract path name
         else {
            System.out.print(" < ");
         }
         
         // print
         System.out.println("abstract path name File/test1.txt");
         
         // prints the value returned by compareTo()
         System.out.print("Value returned: "+value);
         
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt and test1.txt file at the given location.

Lexicographically, abstract path name test.txt < abstract path name File/test1.txt
Value returned: -3

Example 2

The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test1.txt file. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Here, we've changed the order in which the file was compared in previous example. Lastly we're printing the result of comparison.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File f1 = null;
      
      try {
         // create new files
         f = new File("File/test.txt");
         f1 = new File("File/test1.txt");
         
         // returns integer value
         int value = f1.compareTo(f);
         
         // prints
         System.out.print("Lexicographically, ");
         System.out.print("abstract path name test.txt");
         
         // if lexicographically, argument = abstract path name
         if(value == 0) {
            System.out.print(" = ");
         }
         
         // if lexicographically, argument < abstract path name
         else if(value > 0) {
            System.out.print(" > ");
         }
         
         // if lexicographically, the argument > abstract path name
         else {
            System.out.print(" < ");
         }
         
         // print
         System.out.println("abstract path name File/test1.txt");
         
         // prints the value returned by compareTo()
         System.out.print("Value returned: "+value);
         
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt and test1.txt file at the given location.

Lexicographically, abstract path name test.txt > abstract path name File/test1.txt
Value returned: 3

Example 3

The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test2.txt file. Here test2.txt is not present at given location. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Lastly we're printing the result of comparison.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File f1 = null;
      
      try {
         // create new files
         f = new File("File/test.txt");
         f1 = new File("File/test2.txt");
         
         // returns integer value
         int value = f.compareTo(f1);
         
         // prints
         System.out.print("Lexicographically, ");
         System.out.print("abstract path name test.txt");
         
         // if lexicographically, argument = abstract path name
         if(value == 0) {
            System.out.print(" = ");
         }
         
         // if lexicographically, argument < abstract path name
         else if(value > 0) {
            System.out.print(" > ");
         }
         
         // if lexicographically, the argument > abstract path name
         else {
            System.out.print(" < ");
         }
         
         // print
         System.out.println("abstract path name File/test2.txt");
         
         // prints the value returned by compareTo()
         System.out.print("Value returned: "+value);
         
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt file at the given location and test2.txt is not present.

Lexicographically, abstract path name test.txt > abstract path name File/test2.txt
Value returned: -4
java_file_class.htm
Advertisements