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 parent and other parts

farenda 2016-04-22 0

Java Path get parent and other path parts

In this tutorial we’re going to show how to access different parts of java.nio.file.Path from Java NIO.

Get parent directory

Path.getRoot() and Path.getParent() gives access to parts of path:

Path path = Paths.get("/proc/nested/version");
System.out.println("root: " + path.getRoot());
System.out.println("parent: " + path.getParent());

The above code produces the following:

root: /
parent: /proc/nested

Mind that Path.getParent() returns all descendants of the current path – without filename.

Getting parts of path

Using Path.getName() we can access any part of a path:

Path path = Paths.get("/proc/nested/version");
System.out.println("Number of parts: " + path.getNameCount());
System.out.println("top parent: " + path.getName(0));
System.out.println("direct parent: " + path.getName(1));
System.out.println("filename: " + path.getName(2));

The above code produces:

top parent: proc
direct parent: nested
filename: version

Iterate over parts of path

To do that we can use Path.iterator() to iterate over the parts and collect them using Iterator.forEachRemaining(), which is a new default method in Iterator interface. So it’s strictly Java 8 solution.

Path path = Paths.get("/proc/nested/version");

List<Path> parts = new LinkedList<>();
path.iterator().forEachRemaining(parts::add);

if (parts.size() > 1) {
    System.out.println("Parent using iterator: "
        + parts.get(parts.size()-2));
}

Note that object::method notation is Java 8 way of passing object’s method to another method for execution. It simply means that the add method of parts object will be called with each element taken from the iterator.

The above code prints:

Parent using iterator: nested

References:

  • Check out Java IO Tutorial to learn more cool stuff about Java NIO!
Share with the World!
Categories Java Tags java, java-io
Previous: Java filename from Path
Next: Java Path comparison

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