Show TOTAL hidden files in a directory

1

I have this program:

public class TestFileClass {
         public static void main(String[] args) {
                     java.io.File file = new java.io.File("image/us.gif");
                     System.out.println("Existe " + file.exists() );
                     System.out.println("tiene un peso de " + file.length() + " bytes");
                     System.out.println("Puede ser leido? " + file.canRead());
                     System.out.println("Puede ser escrito? " + file.canWrite());
                     System.out.println("Es un directorio? " + file.isDirectory());
                     System.out.println("Es un archivo? " + file.isFile());
                     System.out.println("Es absoluto? " + file.isAbsolute());
                     System.out.println("esta oculto? " + file.isHidden());
                     System.out.println("La ruta absoulta es " +
                     file.getAbsolutePath());
                     System.out.println("Fue modificado por ultima vez en: " +
                     new java.util.Date(file.lastModified()));
         }
}

And now what I need is to show from the hidden files how many there are in total. In other words, the program will read the total of files and I want you to tell me the total but only the hidden ones.

I guess I have to go through it, but I do not quite understand where to enter the hidden or how to do it ... I've done it with the total without taking into account the hidden ones:

File miDir = new File (".");

String[] arregloArchivos = miDir.list();

int numArchivos = arregloArchivos.length;
System.out.println("Total de entradas:" +numArchivos); 

But I do not know if it is correct at all. And now I need the same thing but the hidden ones.

    
asked by Montse Mkd 22.02.2017 в 19:25
source

1 answer

1

There are methods File#listFiles and File#listFiles(FileFilter) that can help you with the slogan.

The first one returns an array with the File s that are within the File current, as long as you are in a folder. You can browse through these files and folders and check if they are hidden:

File[] archivosYCarpetasInternos = miDir.listFiles();
for (File archivoOCarpeta : archivosYCarpetasInternos) {
    //si está oculto, hacer algo...
    if (archivoOCarpeta.isHidden()) {
        /* aquí la lógica para lo que tengas que hacer */
    }
}

The second is used to obtain files and folders that meet certain conditions. Example (use Java 8):

//se envía una expresión lambda que cumple con la definición de la interfaz funcional
//el lambda significa:
//- f: argumento a usar
//- ->: para el argumento a la izquierda, aplicar la
//      funcionalidad de la derecha
// f.isHidden(): devolver el resultado del método File#isHidden
//               para el archivo que estamos visitando
File[] archivosYCarpetasInternos = miDir.listFiles(f -> f.isHidden());
//el valor de archivosYCarpetasInternos serán todos aquellos
//archivos y carpetas que se encuentran dentro de tu carpeta
//y que cumplen con la condición 'File#isHidden' con valor 'true'
    
answered by 23.02.2017 / 02:07
source