文件夹详情和文件夹内文件信息(使用Java)

huangapple go评论69阅读模式
英文:

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());
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月2日 16:55:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63702082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定