Friday, October 10, 2025
HomeLanguagesJavaPath getNameCount() method in Java with Examples

Path getNameCount() method in Java with Examples

The 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.

getNameCount() method of java.nio.file.Path used to return the number of name elements in the path. If this path only represents a root component then method will return 1.

Syntax:

int getNameCount()

Parameters: This method accepts nothing.

Return value: This method returns the number of elements in the path, or 1 if this path only represents a root component.

Below programs illustrate getNameCount() method:
Program 1:




// Java program to demonstrate
// java.nio.file.Path.getNameCount() 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 Path
        Path path
            = Paths.get("D:/workspace/AmanCV.docx");
  
        // call getNameCount()
        int nameCount = path.getNameCount();
  
        // print NameCount
        System.out.println("NameCount in Path: "
                           + nameCount);
    }
}


Output:

NameCount in Path: 3

Program 2:




// Java program to demonstrate
// java.nio.file.Path.getNameCount() 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 Path
        Path path = Paths.get("D:/Resume.pdf");
  
        // call getNameCount()
        int nameCount = path.getNameCount();
  
        // print NameCount
        System.out.println("NameCount in Path: "
                           + nameCount);
    }
}


Output:

NameCount in Path: 2

Program 3:




// Java program to demonstrate
// java.nio.file.Path.getNameCount() 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 Path
        Path path = Paths.get("D:");
  
        // call getNameCount()
        int nameCount = path.getNameCount();
  
        // print NameCount
        System.out.println("NameCount in Path: "
                           + nameCount);
    }
}


Output:

NameCount in Path: 1

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#getNameCount()

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS