Java examples for File Path IO:Path
You can get the number of elements in a path with the getNameCount() method and get the name of each element with the getName() method:
System.out.println("Number of name elements in path: " + path.getNameCount()); for (int i = 0; i < path.getNameCount(); i++) { System.out.println("Name element " + i + " is: " + path.getName(i)); }
import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path = Paths.get("C:", "folder1/folder2/folder4", "test.txt"); System.out.println("Number of name elements is path: " + path.getNameCount()); for (int i = 0; i < path.getNameCount(); i++) { System.out.println("Name element " + i + " is: " + path.getName(i)); }//w w w .java 2 s. c om } }