How to know what files I have in my ArrayList in java?

3

I have a ArrayList<String> which I love files with extensions .pnd .ana .cnf with their respective names. What I need to know is how to ask if there are three extensions within the ArrayList, only the extensions and NO the full file name with its respective extension, for example: find the .pnd and .ana extensions that do nothing, if you find .pnd and .cnf do not do anything but if within the array there are only .pnd extension files, you just have to perform an action , I mean? So far and I do the following:

private void evaluarArchivos() {

        String pnd = ".pnd";
        String ana = ".ana";
        String cnf = ".cnf";
        private boolean archivoExistente = true;

        for (String evaluar : arrayArchivos) {
            if (evaluar.contains(pnd) || evaluar.contains(ana)) {
                archivoExistente = false;
                System.out.println("Nada");
            } 
            if (evaluar.contains(pnd) || evaluar.contains(cnf)) {
                archivoExistente = false;
                System.out.println("Nada 2");
            }
            if (evaluar.contains(pnd)) {
                archivoExistente = true;
                System.out.println("Hacer algo");
            }
        }
    }

I use the contains property but it always returns true since the three extensions are always within the arraylist, and for every turn that my search method gives, it is added what I had plus what is added again.

 public void buscarArchivo(File ruta) {
//        Creo el vector que contendra todos los archivos de una ruta especificada.
        File[] archivo = ruta.listFiles();
//        Evaluo si la carpeta especificada contiene archivos.
        if (archivo != null) {
//            Recorro el vector el cual tiene almacenado la ruta del archivo a buscar.
            for (int i = 0; i < archivo.length; i++) {
                File Arc = archivo[i];
//                Evaluo si el archivo o la ruta es una carpeta.
                if (archivo[i].isDirectory()) {
//                    Le paso la nueva ruta de la carpeta si se cambia la ruta e busca nuevamente.
                    buscarArchivo(archivo[i]);
                } else {
//                    Evaluo el tipo de extencion. 
                    if (archivo[i].getName().endsWith(".pnd") || archivo[i].getName().endsWith(".ana") || archivo[i].getName().endsWith(".cnf")) {
                        contador++;
                        arrayArchivos.add(archivo[i].getName());

//                        System.out.println("Lo que hay en el array es: " + arrayArchivos);
                        evaluarArchivos();
                    }
                }
            }
        }
    }

I think maybe it gives me true because in every turn of my bugle my method searchFile constantly adds new files leaving the old ones that searched for in the previous round, how could I avoid that? It would be that for each lap of the bugle I added to the array, I evaluated it and eliminated it, on the next lap I did the same and so on. Only have in the array the files that are in each round that it gives and not the previous ones . I do not know if I make myself understand ...

Another solution that I thought was to do an if inside the bugle evaluating if not all the extensions end with .pnd by setting my variable archivoExistente = false

                     if (archivo[i].getName().endsWith(".pnd") || archivo[i].getName().endsWith(".ana") || archivo[i].getName().endsWith(".cnf")) {
                        if(!archivo[i].getName().endsWith(".pnd")){
                            // Si no termina con .pnd implica que no todos los archivos son de esa extensión
                             archivoExistente = false;
                             System.out.println("Nada");
                        }
                        contador++;
                        arrayArchivos.add(archivo[i].getName());
                    }

But it does not work for me because it only adds extensions of type .pnd and what I need is to evaluate the type of files inside the folder before adding it or adding all the files that are inside the folder and then evaluate what type of extension they are . I hope you can help me. Thanks

    
asked by Gerardo Ferreyra 12.08.2017 в 04:36
source

3 answers

1

You can use the method. split (".") that has the String class, it will return an array of tokens where you must extract the last element of the array to know what type of file it is, to verify that only a certain code is executed When there are only .pnd files, you can use flags as follows:

private void evaluarArchivos(List arrayArchivos) {
    boolean existePnd = false,
            existeAna = false,
            existeCnf = false;

    for (String archivo : arrayArchivos) {
        String [] palabras = archivo.split(".");
        String ext = palabras[palabras.length() - 1];

        if (ext.equals("pnd"))
            existePnd = true;

        if (ext.equals("ana"))
            existeAna = true;

        if (ext.equals("cnf"))
            existeCnf = true;
    }
    if(existePnd && (!existeAna && !existeCnf)){
        //Codigo si solo existen archivos .pnd
    }
}
    
answered by 12.08.2017 / 06:01
source
1

@Gerardo Ferreyra, I think you always give true for the last if you have,

if (evaluar.contains(pnd)) {
            archivoExistente = true;
            System.out.println("Hacer algo");
        }

In this if questions if there are files with extension ".pnd" but not if they are the only ones that exist, for which you must add the conditionals that the other extensions do not exist:

if (evaluar.contains(pnd) && !evaluar.contains(ana) && !evaluar.contains(cnf)) {
            archivoExistente = true;
            System.out.println("Hacer algo");
        }
    
answered by 12.08.2017 в 19:57
1

Using streams of Java 8 simplifies a lot:

long numeroExtensionesDiferentes = lista.stream()//se crea un stream
                .map(item->item.substring(item.lastIndexOf(".")+1))//quitamos la parte anterior a la extensión
                .distinct()//se eliminan los elementos(extensiones) duplicados
                .count();//se cuenta el número de elementos (extensiones)
    
answered by 12.08.2017 в 12:12