getNameCount()实际计数什么? [英] What does getNameCount() actually count?
问题描述
我的问题分为两部分-首先,标题到底是什么-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;
}
}
为, 要列出目录中的文件,可以使用
As documented, the number of elements in the path, or 0 if this path only represents a root component So in your case, the elements are To list the files in a directory, you can use 这篇关于getNameCount()实际计数什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!"Heather"
,"Desktop"
,"Testing"
和"Testing2"
-不是目录中文件的名称.>
Files.newDirectoryStream(Path)
(在Java 7+中).或者,您可以转换为File
并使用老派" getNameCount()
returns:
"Users"
, "Heather"
, "Desktop"
, "Testing"
and "Testing2"
- not the names of the file within the directory.Files.list(Path)
(in Java 8+) or Files.newDirectoryStream(Path)
(in Java 7+). Or you can convert to a File
and use the "old school" File.listFiles()
method etc.