在Java程序中,如何显示目录中的所有文件?
以下示例显示如何使用File
类的list()
方法显示目录中包含的所有文件。
package com.yiibai;
import java.io.*;
public class DisplayFilesInDirectory {
public static void main(String[] args) {
File dir = new File("F:\\worksp\\javaexamples\\java_directories");
String[] children = dir.list();
if (children == null) {
System.out.println( "Either dir does not exist or is not a directory");
} else {
for (int i=0; i< children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
执行上面示例代码,得到以下结果 -
.classpath
.project
bin
dir11
myfile.txt
src
示例-2
以下是显示目录中所有文件的另一个示例,它显示指定目录及其子目录中以.java为后缀的文件。
package com.yiibai;
import java.io.File;
public class DisplayFilesInDirectory2 {
public static File folder = new File("F:\\worksp\\javaexamples\\java_directories");
static String temp = "";
public static void main(String[] args) {
System.out.println("Reading files under the folder "+ folder.getAbsolutePath());
listFilesForFolder(folder);
}
public static void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
if (fileEntry.isFile()) {
temp = fileEntry.getName();
if ((temp.substring(temp.lastIndexOf('.')
+ 1, temp.length()).toLowerCase()).equals("java"))System.out.println(
"" + folder.getAbsolutePath()+ "\\" + fileEntry.getName());
}
}
}
}
}
执行上面示例代码,得到以下结果 -
Reading files under the folder F:\worksp\javaexamples\java_directories
F:\worksp\javaexamples\java_directories\src\com\yiibai\CreateDirectories.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DeleteDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayRootDirectories.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\EmptyDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\FindCurrentDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchFileInDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\TraversingDirectory.java