如何在目录/文件夹中查找子目录? [英] 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屋!