在Java程序中,如何显示目录中的所有目录?
以下示例显示如何显示一个目录中包含的所有目录,为File
类的list()
方法创建一个过滤器。
package com.yiibai;
import java.io.*;
public class DisplayDirectories {
public static void main(String[] args) {
File dir = new File("F:\\worksp\\javaexamples\\java_directories");
File[] files = dir.listFiles();
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);
System.out.println(files.length +" directories found.");
if (files.length == 0) {
System.out.println("Either dir does not exist or is not a directory");
} else {
for (int i = 0; i < files.length; i++) {
File filename = files[i];
System.out.println(filename.toString());
}
}
}
}
执行上面示例代码,得到以下结果 -
3 directories found.
F:\worksp\javaexamples\java_directories\bin
F:\worksp\javaexamples\java_directories\dir11
F:\worksp\javaexamples\java_directories\src
示例-2
以下是在Java中显示一个目录中的所有目录的另一个示例 -
package com.yiibai;
import java.io.File;
import java.io.IOException;
public class DisplayDirectories2 {
public static void main(String[] args) {
File currentDir = new File("F:\\worksp\\javaexamples\\java_directories\\src");
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
try {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.println("--directory:" + file.getCanonicalPath());
displayDirectoryContents(file);
} else {
System.out.println("----file:" + file.getCanonicalPath());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行上面示例代码,得到以下结果 -
--directory:F:\worksp\javaexamples\java_directories\src\com
--directory:F:\worksp\javaexamples\java_directories\src\com\yiibai
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\CreateDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DeleteDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayDirectories2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayRootDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\EmptyDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\FindCurrentDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchFileInDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\TraversingDirectory.java