Add files between two ArrayList in Java

0

My question is, you can go through two ArrayList<String> causing both to load with a for loop running for a couple of milliseconds triggered with a timer and that first ArrayList loses the focus and go through the second ArrayList<String> making the first one completely emptied in java?

That is, they alternate uploading files. For example, I have the array1 loaded for a few milli seconds with a timer , then load the other array2 making the array1 empty, and so on in that way being able to always have new and different files.

Why do I need this?

I have a method that searches for files and evaluates the types of extensions that are inside a specific folder which are constantly added and deleted, but the method I have is added to a ArrayList , within that array, it is they are constantly adding files every 5 seconds, without being deleted, just add, but within that method buscarArchivos() , I have a condition that is sometimes fulfilled because there are old files stored in ArrayList :

public void buscarArchivo(File ruta) {

           private int contador = 0;
           ArrayList<String> arrayArchivos = new ArrayList<>();

//        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++) {
//                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());
                        evaluarArchivos();
                    }
                }
            }
        }
    }

    private void evaluarArchivos() {
                String pnd = "pnd";
                String ana = "ana";
                String cnf = "cnf";
        boolean existePnd = false,
                existeAna = false,
                existeCnf = false;

        for (String archivo : arrayArchivos) {
//            El punto se usa en las expresiones regulares por lo que si se desea usar como tal se debe definir con "\"
                String[] palabras = archivo.split("\.");
//                Se comvierte el arreglo a un string pandole la longitud completa del arrglo.
                String ext = palabras[palabras.length - 1];
//                Evaluo si es pnd
                if (ext.equals(pnd)) {
                    existePnd = true;
                }
//                Evaluo si es ana
                if (ext.equals(ana)) {
                    existeAna = true;
                }
//                Evaluo si es cnf
                if (ext.equals(cnf)) {
                    existeCnf = true;
                }
            }
//            Pregunto si existe pnd y no ana y no cnf.
            if (existePnd && (!existeAna && !existeCnf)) {
                //Codigo si solo existen archivos .pnd
                System.out.println("Alerta");
            }
    } 

As you will see the code searches for file extensions, as the folder in which I do the search, files with those extensions are constantly added and deleted, making my condition ONLY fulfilled if when the timer starts there are extensions of type .pnd of the folder, if some of the other extensions are added it is also true, BUT if the extensions .ana and .cnf disappear inside the folder my condition is not fulfilled because within the ArrayList there are those extensions stored and what I need is to know if there is any way to alternate the load between two fixes or to empty it, always having different files inside it, Will I explain?

That is, load a Array evaluate content, load another Array with new files added to the folder and empty the previous one.

    
asked by Gerardo Ferreyra 15.08.2017 в 23:03
source

2 answers

0

After about three months and finished my system and I managed to do what I was looking for in this question, this is the answer.

What I wanted to achieve was to remove the existing files from a specified folder and evaluate their extensions, for which use 3 arraylist, in one load all the files with the extensions I need ( .pnd ) , in another position the other files with other extensions ( .cnf .ana ) and in the third arraylist I only load the unique files without repeating their names with unique .pnd extensions, because within the specified folder you can see two or more files with the same name but with different extensions. If this condition is met, it would not add to the third arraylist, which would contain .pnd

answered by 17.09.2017 / 21:14
source
0

I think I have an alternative solution, I do not think it is necessary to use timers, simply use random numbers, the solution is summarized in 3 steps:

  • Obtain number of elements of the first arrangement
  • Get element number of the second array
  • Fill both arrays
  • should look something like this

    ArrayList<String> archivos = getArchivos();
    Random rn = new Random();
    
    //obtener el numero de elementos a almacenar
    int max1 = archivos.size()  + 1;
    int elementos1 = rn.nextInt(max1);
    int elementos2 = archivos.size() - elementos1;
    List<String> archivos1 = new ArrayList<>();
    List<String> archivos2 = new ArrayList<>();
    
    //Llenamos el primer arrayList aleatoriamente
    for(int i=0; i<elementos1; i++){
        Random r = new Random();
        //obtener el elemento aleatorio a ingresar en el arreglo
        int max = archivos.size() + 1;
        int randomNum =  rn.nextInt(max);
    
        //Obtenemos el elemento, lo agregamos a la nueva lista y
        //lo borramos de la original
        String archivo = archivos.get(randomNum);
        archivos1.add(archivo);
        archivos.remove(randomNum);
    }
    
    //Lo mismo va para el segundo
    for(int i=0; i<elementos2; i++){
        Random r = new Random();
        int max = archivos.size() + 1;
        int randomNum =  rn.nextInt(max);
        String archivo = archivos.get(randomNum);
        archivos2.add(archivo);
        archivos.remove(randomNum);
    }
    

    If you want to keep the original list just need to make a temporary copy to do the process that I told you

        
    answered by 16.08.2017 в 06:21