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.