如何在目录/文件夹中找到子目录? [英] How to find sub-directories in a directory/folder?
问题描述
我正在寻找一种方式来获取指定目录中所有目录的名称,而不是文件。
I'm looking for a way to get all the names of directories in a given directory, but not files.
例如,假设我有一个文件夹称为 Parent
,里面我有3个文件夹: Child1
Child2
和 Child3
。
For example, let's say I have a folder called Parent
, and inside that I have 3 folders: Child1
Child2
and Child3
.
我想获取文件夹的名称,但不在乎内容或子文件夹的名称在Child1,Child2等之内。
I want to get the names of the folders, but don't care about the contents, or the names of subfolders inside Child1, Child2, etc.
有没有一个简单的方法?
Is there a simple way to do this?
推荐答案
您可以使用 String [] directories = file.list()
列出所有文件名,
使用循环来检查每个子文件,并使用 file.isDirectory()
函数来获取子目录。
You can use String[] directories = file.list()
to list all file names,
then use loop to check each sub-files and use file.isDirectory()
function to get subdirectories.
例如:
File file = new File("C:\\Windows");
String[] names = file.list();
for(String name : names)
{
if (new File("C:\\Windows\\" + name).isDirectory())
{
System.out.println(name);
}
}
这篇关于如何在目录/文件夹中找到子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!