Search files from a directory

Asked

Viewed 248 times

3

I have a small application that requires a search engine capable of listing the files that contain a term informed by the user.

The first functionality of the program, which is responsible for informing how many times a string appears within a file .txt, is already completed.

Below is the code of the first part:

Teclado teclado= new Teclado();

String opcao= teclado.getUserInput("Insira uma palavra: "); 

BufferedReader br= new BufferedReader(
        new InputStreamReader(
                new FileInputStream("arquivo.txt")));

String linha= br.readLine();
int count=0;
while(linha != null){
    String palavras[] = linha.split(" ");
    for(int i=0; i<palavras.length; i++){
        //System.out.println(palavras[i]);

        if(opcao.equalsIgnoreCase(palavras[i])){  
            count++;  

        }  
    }
    linha= br.readLine();
}

System.out.println(count);

Could someone give me a light on how to implement this search engine?

  • 1
  • Hey, come on, man. List directory files I already know, the same problem is to check which files have a certain string informed by the user. Thank you for the reply.

  • 1

    String inside the file? Or in its name?

  • Inside the archive.

1 answer

4

I managed to solve:

/*
     * Lista arquivos de um determinado diretório.
     */
    String dir= teclado.getUserInput("Insira o diretório: "); 

    File diretorio = new File(dir); 
    File[] arquivos = diretorio.listFiles(); 

    if(arquivos != null){ 
        int length = arquivos.length; 

        for(int i= 0; i< length; i++){ 
            File arquivo = arquivos[i]; 

            if(arquivo.isFile()){  


                /*
                 * Verifica palavras dos arquivos
                 */

                BufferedReader br_d= new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream(arquivo)));
                String linha_d= br_d.readLine();
                int count_d=0;  
                while(linha_d != null){
                    String palavras_d[] = linha_d.split(" ");
                    for(int i_d=0; i_d<palavras_d.length; i_d++){
                        //System.out.println(palavras[i]);

                        if(opcao.equalsIgnoreCase(palavras_d[i_d])){  
                            count_d++;  

                        }  
                    }
                    linha_d= br_d.readLine();
                }
                if(count_d != 0){
                    System.out.println(arquivo.getName()); 
                    System.out.println(count_d);
                }


                /*
                 * FIM
                 */
            } 
            else 
                if(arquivo.isDirectory()){ 
                System.out.println("Diretorio: " + arquivo.getName()); 
            } 
        } 
    }  

Now I just need a more refined search to list only files . txt, but otherwise all beauty. :)

Browser other questions tagged

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