The Paths interface overrides equals() and compareTo(), allowing us to compare paths in the file system.
import java.nio.file.Paths fun main(args : Array<String>){ //Get two references to the home directory val home = Paths.get(System.getProperty("user.home")) val otherHome = Paths.get(System.getProperty("user.home")) //Get the current working directory val cwd = Paths.get(System.getProperty("user.dir")) println("$home == $otherHome is " + (home == otherHome)) println("$home == $cwd is " + (home == cwd)) println("$home < $cwd is " + (home < cwd)) println("$cwd < $home is " + (cwd < home)) println("$home >= $otherHome is " + (home >= otherHome)) }
Output
/Users/stonesoup == /Users/stonesoup is true /Users/stonesoup == /Users/stonesoup/IdeaProjects/OCJAP is false /Users/stonesoup < /Users/stonesoup/IdeaProjects/OCJAP is true /Users/stonesoup/IdeaProjects/OCJAP < /Users/stonesoup is false /Users/stonesoup >= /Users/stonesoup is true
compareTo(other : Path) : Int
The javadoc states that two paths are compared lexicographically as defined by the file system provider. The default provider uses a platform specific means of comparing file paths.
equals(other : Object) : Boolean
Assuming the other is a Path object and is not associated with a different file system, the paths are equal if they are the same path. Keep in mind that case sensitivity may apply. Mac OS X is case insensitive so “belchers.txt” is the same as “Belchers.TXT”, but on Linux, this would be false.
References
https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.htm