Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Path comparison

farenda 2016-04-25 0

Java Path comparison

In this tutorial we’re going to show how to compare Java Path with other paths and strings. Very useful when finding files on filesystem.

Compare to other Path/string

Path.compareTo() methods compare paths lexicographically and don’t access file system to check whether file exists or not.

Compare to different path

Path path = Paths.get("/proc/version");
Path other = Paths.get("/vmlinuz");
System.out.printf("%s.compareTo(%s): %d%n",
    path, other, path.compareTo(other));

Result:

/proc/version.compareTo(/vmlinuz): -6

Compare to relative path

Path path = Paths.get("/proc/version");
Path other = Paths.get("/proc/./version");
System.out.printf("%s.compareTo(%s): %d%n",
    path, other, path.compareTo(other));

Result:

/proc/version.compareTo(/proc/./version): 72

Redundant elements are not removed, so use Path normalization before comparison.

Compare to the same path

Path path = Paths.get("/proc/version");
Path other = Paths.get("/proc/version");
System.out.printf("%s.compareTo(%s): %d%n",
    path, other, path.compareTo(other));

Result:

/proc/version.compareTo(/proc/version): 0

Path ends with something

Path.endsWith(Path)

Path path = Paths.get("/proc/version");
Path version = Paths.get("version");
System.out.printf("%s.endsWith(%s): %s%n",
    path, version, path.endsWith(version));

Result:

/proc/version.endsWith(version): true

Path.endsWith(“string”)

Path path = Paths.get("/proc/version");
System.out.printf("%s.endsWith(\"version\"): %s%n",
    path, path.endsWith("version"));

Result:

/proc/version.endsWith("version"): true
Path path = Paths.get("/proc/version");
System.out.printf("%s.endsWith(\"something\"): %s%n",
    path, path.endsWith("something"));

Result:

/proc/version.endsWith("something"): false

Path starts with Path/string

Path.startsWith(Path)

Path path = Paths.get("/proc/version");
Path other = Paths.get("/proc");
System.out.printf("%s.startsWith(%s): %s%n",
    path, other, path.startsWith(other));

Result:

/proc/version.startsWith(/proc): true

Path.startsWith(“string”)

Path path = Paths.get("/proc/version");
System.out.printf("%s.startsWith(\"/proc\"): %s%n",
    path, path.startsWith("/proc"));

Result:

/proc/version.startsWith("/proc"): true

References:

  • check out Java IO Tutorial to learn more about Java NIO!
Share with the World!
Categories Java Tags java, java-io
Previous: Java Path parent and other parts
Next: Clojure mock Java class

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored