Related Articles

Related Articles

Path compareTo() method in Java with Examples
  • Last Updated : 16 Jul, 2019

The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is an absolute path and other is a relative path. The absolute path is the location address from the root to the entity while the relative path is the location address which is relative to some other path.

compareTo(java.nio.file.Path) method of java.nio.file.Path used to compare two abstract paths lexicographically. Two paths can be compared by this method. The ordering of paths defined by this method is provider specific, and in the case of the default provider, platform specific. This method returns zero if the argument is equal to this path, a value less than zero if this path is lexicographically less than the argument, or a value greater than zero if this path is lexicographically greater than the argument. This method does not access the file system. There is no need that file is required to exist. This method may not be used to compare paths that are associated with different file system providers.

Syntax:

int compareTo(Path other)

Parameters: This method accepts a single parameter another path which is the path compared to this current path.

Return value: This method returns zero if the argument is equal to this path, a value less than zero if this path is lexicographically less than the argument, or a value greater than zero if this path is lexicographically greater than the argument.



Exception: This method throw and Exception ClassCastException if the paths are associated with different providers.

Below programs illustrate compareTo(java.nio.file.Path) method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Path.compareTo(Path) method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Paths
        Path path1
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        Path path2
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        // compare paths
        int value = path1.compareTo(path2);
  
        // print result
        if (value == 0)
            System.out.println("Both are equal");
        else if (value < 0)
            System.out.println("Path 2 is greater "
                               + "than path 1");
        else
            System.out.println("Path 1 is greater "
                               + "than path 2");
    }
}

chevron_right


Output:

Both are equal

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Path.compareTo(Path) method
  
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // create object of Paths
        Path path1
            = Paths.get("D:\\eclipse\\configuration"
                        + "\\org.eclipse.update");
  
        Path path2
            = Paths.get("D:\\temp\\Spring");
  
        // compare paths
        int value = path1.compareTo(path2);
  
        // print result
        if (value == 0)
            System.out.println("Both are equal");
        else if (value < 0)
            System.out.println("Path 2 is greater "
                               + "than path 1");
        else
            System.out.println("Path 1 is greater "
                               + "than path 2");
    }
}

chevron_right


Output:

Path 2 is greater than path 1

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#compareTo(java.nio.file.Path)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :