Path compareTo() method in Java with Examples
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:
// 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"); } } |
Both are equal
Program 2:
// 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"); } } |
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)
Recommended Posts:
- Boolean compareTo() method in Java with examples
- FloatBuffer compareTo() method in Java With Examples
- DoubleBuffer compareTo() method in Java With Examples
- Calendar compareTo() Method in Java with Examples
- Date compareTo() method in Java with examples
- Instant compareTo() method in Java with Examples
- LocalTime compareTo() method in Java with Examples
- MonthDay compareTo() method in Java with Examples
- Double.compareTo() Method in Java with Examples
- Float compareTo() method in Java with examples
- Charset compareTo() method in Java with Examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- Byte compareTo() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



