gpt4 book ai didi

java - getNameCount() 实际上算什么?

转载 作者:行者123 更新时间:2023-11-29 10:13:48 25 4
gpt4 key购买 nike

我的问题分为两部分 - 首先,标题到底是什么 - 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;
}
}

最佳答案

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

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

关于java - getNameCount() 实际上算什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com