getNameCount() 实际上算什么?

Hea*_*r T 1 java filenames path

我的问题有两部分 - 首先,标题到底是什么 - Path.getNameCount() 方法实际计数的是什么?在Eclipse中选择方法时,我阅读了它附带的小弹出信息,我认为这是一个合适的用法。我使用它创建的这个方法在运行时返回 5 作为 int 。其次,我想要做的是返回目标目录中有多少文件,以便我可以运行另一种方法,我必须以适当的次数检索文件名。如果 getNameCount() 方法不适合此功能,您是否有任何关于如何完成相同目的的建议?

//Global Variable for location of directory
Path dir = FileSystems.get("C:\\Users\\Heather\\Desktop\\Testing\\Testing2");
//Method for collecting the count of the files in the target directory.
public int Count()
{
    int files=0;
    files = dir.getNameCount();
    return files;
}
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*urg 5

getNameCount()返回路径(子目录)中的元素数。例如,在 Windows 中,“C:”将是 0,而对于“C:\a\b\c” - 3,在类 Unix 系统中,根 ("/") 将处于 0 级 ( getNameCount() == 0)并且“/home/user/abacaba”将在第 3 级,请参阅javadoc

要列出您应该使用的目录DirectoryStreamjavadoc - 这里给出了完美的例子。