FileFilter problem with the if-else

0

I have a problem, I guess many hours programming but I do not know how to see, and surely that is the dumbest thing in the world.

I have the following program:

FileFilter directoryFilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
                };
        File[] files = miDir.listFiles(directoryFilter);
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.print("directorio:");
                        } else{
                                System.out.print("   archivo:");
                                 // Mostrar archivos ocultos  
                                 int total=0;
                                 File[] archivosYCarpetasInternos = miDir.listFiles();
                                 for (File archivoOCarpeta : archivosYCarpetasInternos) {//si esta oculto..
                                 if (archivoOCarpeta.isHidden()) { 
                                 // System.out.println(ficheros[x].getName()); Por si queremos mostrar cuales son... 
                                 //Aquí lo que hacemos..
                                  total ++; }      
                                    }System.out.println("Número de fitxers ocults :"+total); 

            }
            System.out.println(file.getCanonicalPath());}

What I want to achieve is with the FileFilter to divide my program in two parts by one part directories and by another files.

For now the part of files inside the perfect if it shows me the 4 files there are, but I want to do the same with the else, that shows me the files.

I imagine that the error is there but I do not know how to see it.

thanks!.

    
asked by Montse Mkd 23.02.2017 в 15:23
source

2 answers

1

This should work. You were not counting the directories:

        for (File file : files) {
            int totalDir=0;
            if (file.isDirectory()) {
                totalDir ++;
                System.out.print("directorio:");
            } else {
                System.out.print("   archivo:");
                // Mostrar archivos ocultos  
                int total=0;
                File[] archivosYCarpetasInternos = miDir.listFiles();
                for (File archivoOCarpeta : archivosYCarpetasInternos){
                //si esta oculto..
                     if (archivoOCarpeta.isHidden()) { 
                         // System.out.println(ficheros[x].getName()); Por si queremos mostrar cuales son... 
                         //Aquí lo que hacemos..
                         total ++; 
                      }      
                 }
System.out.println("Número de directorios :"+totalDir); 
System.out.println("Número de fitxers ocults :"+total); 
    
answered by 23.02.2017 / 16:11
source
0

Why do not you try this:

 for (File file : files) {

          if(file .getPath().equalsIgnoreCase("ruta de directorio")){
  //codigo restante
}
     if(file .getPath().equalsIgnoreCase("ruta de archivos")){
  //codigo restante
}
}}

I set an if inside the loop one for directories and another for files

    
answered by 23.02.2017 в 15:57