getNameCount()实际计数什么? [英] What does getNameCount() actually count?

查看:296
本文介绍了getNameCount()实际计数什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

"Heather""Desktop""Testing""Testing2"-不是目录中文件的名称.

要列出目录中的文件,可以使用

As documented, getNameCount() returns:

the number of elements in the path, or 0 if this path only represents a root component

So in your case, the elements are "Users", "Heather", "Desktop", "Testing" and "Testing2" - not the names of the file within the directory.

To list the files in a directory, you can use 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.

这篇关于getNameCount()实际计数什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆