英文:
Folder details and files inside the folder info using Java
问题
我有一个文件夹结构,类似于:E:\Test\。在里面,我有许多子文件夹,如FolderA、FolderB、FolderC等。
我想要一个Java程序,可以列出所有子文件夹和子文件夹内的报告文件。我该如何实现?使用下面的代码片段,我可以访问E:\Test目录内的不同文件夹,但子文件夹内的文件并没有显示。
public static void main(String args[]){
File directoryPath = new File("E:\\Test\\");
File folderPath [] = directoryPath.listFiles();
System.out.println("List of files and folders in the directory : ");
for(File file : folderPath){
System.out.println("Folder Name Is : " + file.getAbsolutePath());
System.out.println("Files under the folderpath are : " + file.listFiles());
System.out.println(" ");
}
}
英文:
I have a folder structure like : E:\Test. Inside it I have many sub-folders like FolderA, FolderB, FolderC, etc.
I want to have a java program that will list down all the subfolders and the report files inside the subfolders. How can I achieve this? Using the below snippet, I can access the different folders inside the E:\Test directory, but files inside the subfolders aren't appearing.
public static void main(String args[]){
File directoryPath = new File("E:\\Test\\");
File folderPath [] = directoryPath.listFiles();
System.out.println("List of files and folders in the directory : ");
for(File file : folderPath){
System.out.println("Folder Name Is : " +file.getAbsolutePath());
System.out.println("Files under the folderpath are : " +file.listFiles());
System.out.println(" " );
}
}
答案1
得分: 0
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
private static final StringBuilder tabs = new StringBuilder();
public static void main(String[] args) {
try {
Path start = new File("E:\\Test\\").toPath();
System.out.println("List of files and folders in the directory " + start.getFileName() + ":");
Files.walk(start).forEach(Main::printInfo);
} catch (IOException ignore) {
}
}
public static void printInfo(Path path) {
String tabs = Main.tabs.toString();
if (Files.isDirectory(path)) {
System.out.println(tabs + "Folder Name Is: " + path.getFileName());
System.out.println(tabs + "Files under the " + path.toString() + ":");
Main.tabs.append("\t");
} else {
System.out.println(tabs + path.getFileName());
}
}
}
英文:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
private static final StringBuilder tabs = new StringBuilder();
public static void main(String[] args) {
try {
Path start = new File("E:\\Test\\").toPath();
System.out.println("List of files and folders in the directory " + start.getFileName() + ": ");
Files.walk(start).forEach(Main::printInfo);
} catch (IOException ignore) {
}
}
public static void printInfo(Path path) {
String tabs = Main.tabs.toString();
if (Files.isDirectory(path)) {
System.out.println(tabs + "Folder Name Is : " + path.getFileName());
System.out.println(tabs + "Files under the " + path.toString() + ": ");
Main.tabs.append("\t");
} else {
System.out.println(tabs + path.getFileName());
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论