Number of folders and files per directory, Java

0

I have the following function that calculates the amount of directories and files that are inside each one (now it is a bit modified, but the idea was that).

public class ComponentCompare {

    private static int numDir;
    private static int numFiles;

    public static void main(String[] args) {
 try {
            numDir = 0;
            numFiles = 0;
            recursive(new File("/home/incentivate/Desktop/resources")); // Directorio raíz
            //System.out.println("\nSe han encontrado: " + numDir + " directorios");
            //System.out.println("\nSe han encontrado: " + numFiles + " archivos");
            File salida = new File("./salida.txt");
            BufferedWriter bw = new BufferedWriter(new FileWriter(salida));
            bw.write("Se han encontrado: " + numDir + " directorios \n");
            bw.write("Se han encontrado: " + numFiles + " archivos \n");
            bw.close();
        } catch (IOException e) {
            System.out.println("No existe el archivo de salida!");
        }

    } // end del main()



 public static void recursive(File dir) {

        File listFile[] = dir.listFiles();
        File listDir[];
        //System.out.println("Dir:" + dir);

        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()){
                    listDir = listFile[i].listFiles();
                    System.out.println("Hay " + listDir.length + " archivos dentro de " + listFile[i].toString());
                    recursive(listFile[i]);
                    numDir++;
                } else if(listFile[i].isFile()){
                    numFiles++;
                }
            }
        }
    } // end de recursive()

That returns this to me:

  

There are 6 files inside / home / incentivate / Desktop / resources / static

     

There are 2 files inside / home / incentivate / Desktop / resources / static / icons

     

There are 6 files inside / home / incentivate / Desktop / resources / static / gifs

     

There are 2 files inside / home / incentivate / Desktop / resources / static / vendor

     

There are 5 files inside / home / incentivate / Desktop / resources / static / vendor / materialize

     

There are 2 files inside / home / incentivate / Desktop / resources / static / vendor / materialize / font

     

There are 15 files inside / home / incentivate / Desktop / resources / static / vendor / materialize / font / roboto

     

There are 6 files inside / home / incentivate / Desktop / resources / static / vendor / materialize / font / material-design-icons

     

There are 2 files inside / home / incentivate / Desktop / resources / static / vendor / materialize / js

     

There are 2 files inside / home / incentivate / Desktop / resources / static / vendor / materialize / css

     

There are 1 files inside / home / incentivate / Desktop / resources / static / vendor / jquery

     

There are 5 files inside / home / incentivate / Desktop / resources / templates

My problem is that I need you to tell me for each folder you enter, what's inside it ... if there are X amount of files and number of folders I need you to list that.

Example:

Go to the folder A --- > Within this there are 10 more folders and 5 files. That you list this.

After I enter folder B and tell me exactly the same and so for each one.

Right now it works like what I want, but not exactly.

Can someone help me out?

Thank you very much in advance!

Greetings to all

    
asked by Nacho Zve De La Torre 20.09.2018 в 19:47
source

1 answer

2

The first thing that comes to my mind (there could be something better maybe) that you could do is this:

public static void recursive(File dir) {

        File listFile[] = dir.listFiles();
        File listDir[];

        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()){
                    listDir = listFile[i].listFiles();
                    files = Arrays.stream(listDir).filter(x -> x.isFile()).toArray();    
                    // Usando filder, optienes el array de archivos

                    System.out.println("Hay " (listDir.length - files.length) + " carpetas y " + files.length + " archivos dentro de " + listFile[i].toString());
                    //La salida seria "Hay xCant carpetas y xCant archivos dentro de xPath"

                    recursive(listFile[i]);
                    numDir++;
                } else if(listFile[i].isFile()){
                    numFiles++;
                }
            }
        }
    }

By using .filter() , you get the array of files within your directory by validating .isFile() .

I assume you are using Java 8+, but you will not have the .filter()

    
answered by 20.09.2018 / 20:22
source