在本机的磁盘系统中找一个文件夹利用File类的提供方法列出该文件夹中的所有文件的文件名和文件的路径java
您可以使用以下代码来列出文件夹中的所有文件的文件名和文件路径:
import java.io.File;
public class FileListExample {
public static void main(String[] args) {
// 指定文件夹路径
String folderPath = "C:\\path\\to\\folder";
// 创建File对象
File folder = new File(folderPath);
// 判断文件夹是否存在
if (folder.exists() && folder.isDirectory()) {
// 获取文件夹中的所有文件
File[] files = folder.listFiles();
// 遍历文件数组
for (File file : files) {
// 输出文件名和文件路径
System.out.println("文件名:" + file.getName());
System.out.println("文件路径:" + file.getAbsolutePath());
System.out.println("--------------------------");
}
} else {
System.out.println("文件夹不存在或不是一个文件夹。");
}
}
}
请替换folderPath变量的值为您要列出文件的文件夹路径。运行此代码,将会输出文件夹中所有文件的文件名和文件路径
原文地址: https://www.cveoy.top/t/topic/iqNZ 著作权归作者所有。请勿转载和采集!