Display all directories and files contained in them

Asked

Viewed 1,021 times

1

I’m trying to mount a file tree where in addition to displaying the computer directories is displayed also your files. The code I made below is listing only the specified directory, in case the C:\. But the directories contained within the C: such as "Program Files" does not display what is inside.

package arvore;

import java.io.File;

public class Arvore {

public static void main(String[] args) {

    File folder = new File("C:\\");

    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            System.out.println("Este é Arquivo " + listOfFiles[i].getName());
        } else if (listOfFiles[i].isDirectory()) {
            System.out.println("***ESTE E DIRETÓRIO " + listOfFiles[i].getName());
            }
        }

    }
}

The question is how would it look to show everyone including subdirectories.

2 answers

0


Uses recursion:

public static void main(String[] args) {

    File folder = new File("C:\\");

    for (File arquivo : folder.listFiles()) {
        listarArquivos(arquivo);
    }

}

public static void listarArquivos(File raiz) {
    if (raiz.isFile()) {
        System.out.println("Este é Arquivo " + raiz.getName());
        return;
    }
    System.out.println("***ESTE E DIRETÓRIO " + raiz.getName());
    for (File no : raiz.listFiles()) {
        listarArquivos(no);
    }

}

0

This one lists everything! Subdirectories, and all files.

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;


public class Teste {

    public static void main(String[] args) {
        try {
            Path startPath = Paths.get("c:/");
            Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir,
                        BasicFileAttributes attrs) {
                    System.out.println("Diretório: " + dir.toString());
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    System.out.println("Arquivo: " + file.toString());    
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException e) {
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}   
  • Yes really I took a look at it and it lacks nothing, the question is... What if I had more than one directory ? It would show everyone and their respective directories?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.