Browse recursive directories in JAVA

Asked

Viewed 701 times

-1

I’m doing a work for the college on how to go through directories with java recursive and so far I have this code here:

    package Control;

import java.awt.Desktop;
import java.io.*;

public class PercorreDir {

    private String pastas = "";
    StringBuffer buffer = new StringBuffer();

    public String percorre(String caminho, String espaço){

        File documentos = new File(caminho);

        for( File files : documentos.listFiles()){
            if(files.isDirectory()) {

                buffer.append(files + espaço);
                percorre(caminho, espaço);
            }
        }

        return pastas;

    }
}

What I want to know is how do I go through the subfolders as well. Thank you.

  • The above method already does what you said. This question makes no sense. Also, please avoid creating multiple questions for the same problem. If the answer to the first question did not resolve, improve the question by editing it and adding relevant information. You can also add comments to the answers by telling them why they don’t solve the problem.

  • This question already has an answer here http://answall.com/questions/101904/display-direct%C3%B3rios-in-tree format and also here http://answall.com/questions/101772/recursiva-java-para-ver-pastas-e-subpastas

1 answer

1


Hello, I found a code that can be didactic for your learning about recursion. The code below serves to search for a file and it searches in all folders/subpasatas.

   package com.mkyong;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    public class ProcurarArquivo {

      private String nomeParaBuscar;
      private List<String> resultado = new ArrayList<String>();

      public String getNomeDoArquivoParaBuscar() {
        return nomeParaBuscar;
      }

      public void setNomeDoArquivoParaBuscar(String nomeParaBuscar) {
        this.nomeParaBuscar = nomeParaBuscar;
      }

      public List<String> getResultado() {
        return resultado;
      }

      public static void main(String[] args) {

        ProcurarArquivo procurarArquivo = new ProcurarArquivo();

        //tente diferentes diretorios.
        procurarArquivo.procurarPasta(new File("/var/www/html"), "post.php");

        int count = procurarArquivo.getResultado().size();
        if(count ==0){
            System.out.println("\nNão foi encontrado!");
        }else{
            System.out.println("\nencontrado " + count + " resultados!\n");
            for (String bateu : procurarArquivo.getResultado()){
            System.out.println("encontrado : " + bateu);
            }
        }
      }

      public void procurarPasta(File diretorio, String nomeParaBuscar) {

        setNomeDoArquivoParaBuscar(nomeParaBuscar);

        if (diretorio.isDirectory()) {
            procurar(diretorio);
        } else {
            System.out.println(diretorio.getAbsoluteFile() + " não é um diretório!");
        }

      }

      private void procurar(File arquivo) {

        if (arquivo.isDirectory()) {
          System.out.println("procurando pasta ... " + arquivo.getAbsoluteFile());

            //você tem permissão para ler?
            if (arquivo.canRead()) {
            for (File temp : arquivo.listFiles()) {
                if (temp.isDirectory()) {
                procurar(temp);
                } else {
                if (getNomeDoArquivoParaBuscar().equals(temp.getName().toLowerCase())) {            
                    resultado.add(temp.getAbsoluteFile().toString());
                }

            }
            }

         } else {
            System.out.println(arquivo.getAbsoluteFile() + "Permissão negada");
         }
          }

      }

    }

Browser other questions tagged

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